1. PF Contest - Win "Conquering the Physics GRE" book! Click Here to Enter
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Binary Numbers

  1. Feb 5, 2017 #1
    1. The problem statement, all variables and given/known data

    So in C an integer is stored as a 32 byte value. Were the 32nd byte is the byte that determines if the value is negative or positive. I created a loop were I set the integer i equal to 1. Each time I cycle through the loop I left shift the variable i by one.

    Meaning that the first time through the loop the integer i becomes 10, second time becomes 100. I'm going through a while loop until the variable i becomes negative. Apparently on the 32nd time through the loop I exit the loop. I print the variable i and i get negative 2^(31) power.

    2. Relevant equations

    3. The attempt at a solution

    I don't fully understand this. On the 31st time through the loop I have something like
    1000000000000000000000000000000 or positive 2^31
    so when I cycle through the loop a 32nd time and left shift the integer i to the left one shouldn't i just get
    10000000000000000000000000000000 or negative zero?
    Why do I appear to be getting
    1100000000000000000000000000000 or negative 2 to the 31 power?
    Does the compiler see negative zero as the most negative possible number meaning -2^31? That makes no since to me. When I think of negative zero, it makes no since, and generally think of a very small negative number, meaning I'm approaching zero from the negative side but "never actually reaching zero". Something like negative zero is -0.00000000000000000000001?
  2. jcsd
  3. Feb 5, 2017 #2


    User Avatar
    Gold Member

    You mean BIT, not BYTE.

    As for the rest, the 2's complement number system used by modern computers does not HAVE the "negative zero" that you seem to have expected, but I don't understand your results. A true shift is not a signed operation. Are you really doing a shift or are you doing a multiply? Your code should be extremely short. Why not just post it?
  4. Feb 5, 2017 #3


    Staff: Mentor

    Where the 32nd bit is the bit...
    No, that's not how it works. 2^31 in hex is 0x7FFF, or in binary, 0111 1111 1111 1111 1111 1111 1111 1111.
    If you shift left by one bit you get 0xFFFF, or 1111 1111 1111 1111 1111 1111 1111 1111
    Since the high bit is set (equals 1), this is now a negative number. In fact, for a 32-bit int, this is -32,768. It is NOT "negative zero."
  5. Feb 5, 2017 #4


    User Avatar
    Gold Member

    But if he is really doing a shift, it shouldn't matter what the number system is. A shift is logical operation, not a numerical operation, and shifting the last bit off to the left should get all 0's.

    That is, you are explaining the number system to him, which is likely to be useful to him but does not answer his question.

    As I said, I don't understand his result. Having the next-to-left-most bit a 1 and then having BOTH of the left-most bits be one makes no sense to me as the result of a logical operation.
    Last edited: Feb 5, 2017
  6. Feb 5, 2017 #5
    Sorry for the late reply. Thanks for your help. My code is

    int y, z=1;

    while (y > 0){

    y=y << 1;

    z=z + 1;


    I then printf y, and z. For Y i get -2^(31), and for z I get 32. I don't understand this result.
    eventually I get to
    then one more time through I get
    it sets the signed number that sets it to be negative to be 1, but why for Y do I get -2^(31) I have no idea. It exits the loop once y is less than or equal to zero. But I don't get the -2^(31).
    I was writing in C and using a compiler. It's been a few years since I messed with C. But from what little I know, it keeps shifting over one bit, eventually it reaches the max number of bits that it can store for one integer, and then the left shift on this value shifts the 1 over to the bit that sits the sign. So I would have something like -0, but it doesn't understand negative zero, but I don't understand why it would give me -2^(31).
  7. Feb 5, 2017 #6


    Staff: Mentor

    Explained in post #3. For int values, there is no "negative zero."
  8. Feb 5, 2017 #7


    User Avatar
    Gold Member

    (1) Learn to use the code tags
    (2) Make up your mind. First you said you got the left-most two bits BOTH one and now you say you only get the leftmost bit to be one. Which is it?
    (3) Learn the two's complement number system
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted