Some basic C++ concepts

This is a discussion on Some basic C++ concepts within the C++ Programming forums, part of the General Programming Boards category; My professor told me that the concepts is extremely important than programming.So today he shows us: char c=0; c=~c;(which is ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    67

    Some basic C++ concepts

    My professor told me that the concepts is extremely important than programming.So today he shows us:

    char c=0;
    c=~c;(which is the bitwise)

    so what is c=?

    Since C is 00000000,and use not(~) so it is 11111111 now,but he said the answer is -1 instead of 255.Because he says it use one's complement or something~~~so,why it is -1?
    (I know it can't be 255,since char is 1 byte with sign,which means it is -128~127,so 255 is out of range,so why it is -1?)

    Ok,guys I know I am so stupid since for storing int ,we use two's complement notation,so 1111 1111 means -1.
    but I want to know We also ues Excess notation for int,so why this case we use two's complement notation instead of Excess notation?which situations we use Excess notation? thx!
    Last edited by tx1988; 09-13-2007 at 12:05 AM.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    67
    and also for this :

    we know 0111 1111 is 127,how about 0111 1111+1 if the variable is char(which is -128~127)?
    so because it is 1000 0000 after +1,so 1 is use for sign(-),why the answer is -128?


    also for this one,we use two's complement so 1000 0000 means the least num!

    So I know I am so stupid,since When we study this in CS 2,our teacher just go through it so quickly and we didn't focus on it since he thinks programming is more important.I know it is an excuse and I should be good at it.
    Last edited by tx1988; 09-13-2007 at 12:08 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    matching values to bit patterns isn't really basic C++, it's implementation-specific C++, and this is specicfic to twos complement bit encodings.

    twos complement works a little differently than regular binary math. negative numers are ordered backwards.

    http://en.wikipedia.org/wiki/Twos_complement

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > but he said the answer is -1 instead of 255
    It's the same bit pattern, do you regard 'char' as being signed or unsigned ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh dear. Your teacher makes a LOT of unwarranted assumptions here. Namely:
    1) "C is 00000000"
    No. There's no guarantee that char has 8 bits, even though that's the most common number.
    2) "the answer is -1 instead of 255"
    No. Whether char is signed or unsigned is implementation-defined. That's why char, signed char and unsigned char are distinct types. (Unlike the other integral types, where signed xxx and xxx are the same.) Thus, it is implementation-defined whether the result is 255 (unsigned) or ... something else (signed).
    3) "the answer is -1"
    No. It is implementation-defined (or even unspecified) what negative representation is used, with the only restriction being that an all-zero bit pattern must represent 0. Thus, 2's complement, 1's complement and sign-value are all valid (and have been used at least once), whereas excess representation is invalid (0 is not all-zero).
    4) "so it is 11111111 now"
    No. That is, not if char is signed and another common condition holds: then it's undefined behaviour. The reason is that ~ does integral promotion, so the result of ~c is actually an int, and all ones at that. The other common condition mentioned above is that sizeof(int) > sizeof(char). If that's the case, then the assignment back to c constitutes a signed overflow, which is UB.

    So ... to answer the original question, "what is c?"
    If char is signed, the question cannot be answered, since c = ~c is undefined behaviour.
    If char is unsigned, c is CHAR_MAX.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by CornedBee View Post
    The other common condition mentioned above is that sizeof(int) > sizeof(char).
    I think the C standard mandates that his is true.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    I think the C standard mandates that his is true.

    QuantumPete
    It thought the mandated point was sizeof(int) >= sizeof(char).

    As to the point of Cornedbee, the behaviour of c = ~c may well technically be undefined for a signed char, but I have yet to see a system where it's not resulting in a -1, and I have worked on several processor architectures: x86, 68K, 29K, ARM, 6502, Z80, 8031, VAX, PDP-11. All of which show this behaviour. So whilst it's technically correct that the result is undefined, it is also quit unusual that the result is something OTHER than -1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > If char is unsigned, c is CHAR_MAX.

    I think this also relies on the fact that an unsigned char is guaranteed not to have any padding bits (isn't it UB to modify padding bits?). I'm not sure what, if any, guarantees regarding padding bits exist for signed char (specifically, in C90, C99, and C++).

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by QuantumPete View Post
    I think the C standard mandates that his is true.

    QuantumPete
    No, it doesn't - it's guaranteed that 1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long), but it's possible that they could all be equal to 1 (though this would require a char to have at least 32 bits, which is allowed, since the standard doesn't imply any upper limit on CHAR_BIT, only a lower limit of 8).

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by robatino View Post
    No, it doesn't - it's guaranteed that 1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long),
    my mistake
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    tx1988,

    Binary is very difficult for beginners, simply because cin and cout don't support binary, and you can't see what you are doing!

    It requires a bit more code to display binary. If I were teaching the course, I would assign you to write a program that displays in binary, before getting into bit manipulation. You might want to try that in your "spare time".

    This can be done "the hard way", by writing your own functions). Or, this is fairly easy to do with <bitset>. And, you can input numbers in almost any base (formatted as a string) with strtol().

    In the "real world", people are rarely interested in the integer or decimal value when manipulating bits. It sounds a bit strange, but although it is clearly a number we don't care about it's value. I suppose it's like ASCII characters... They are numbers, but they represent characters.

    When we want to "see" the "bit pattern", we will usually display it as (unsigned) hexadecimal number. It is much easier to convert between hex and binary, than between decimal and binary. With a little practice, you can learn to convert numbers of any size in your head. (You only have to memorize 16 conversions... You already know 0 and 1.

    My professor told me that the concepts is extremely important than programming.
    Yes, it is important to know, and every college-level computer science major has to be very comfortable with binary (and hex). However, it's not something that most programmers use every day. It's of more use to hardware guys (like me) and to progrmmers who program "close to the hardware"... programmers who write drivers & firmware, or who program in assembly language.

    in CS 2,our teacher just go through it so quickly
    Yes, I would expect college-level programming to be very fast paced, and to require more homework-time than your average class. But, I would have expected CS1 to be even worse, since most of the concepts are new to most students. The "introductory" classes are usually the hardest, in any subject!

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    CS1 was a lot of hand holding in my experience, CS2 was when they started teaching real programming, and that is when most students find out they are not cut out for it, after that, it is the second year classes that separate the programmers and the people who are just kinda interested in computers.

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    67
    SO,now You guys mean that I should continue working hared on the concepts or just simply know something about it.Because My professor told me to be a computer science major,we should be good at programming,concepts,hardware,circuits,and of course calculus.In fact,I am interested in how to programming and how to use software more than knowing how it works,because I maybe want to be a statistic major who also good at computer software(like using SPSS,SAS or VB , VC )
    support my work in data analyse and statistic like working as a Actuary(which is one of the most
    hardest major in the world requiring a lot of hard exams and many years for studying) .


    Can some of you guys who is former programmer tell me what courses needed for CS major and
    is it easy or hard to be good at CS major~~~?
    THX for your experience!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. basic Mathmatics and Algebraic concepts
    By incubusnb in forum C++ Programming
    Replies: 8
    Last Post: 05-22-2005, 06:53 PM
  4. Basic 2d Programming Concepts
    By JoshG in forum Game Programming
    Replies: 4
    Last Post: 05-17-2002, 05:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21