Understanding Binary Numbers & Homework on Negative Zero

In summary: On the 31st time through the loop, you have something like1000000000000000000000000000000 or positive 2^31so when you cycle through the loop a 32nd time and left shift the integer i to the left, i should just get10000000000000000000000000000000 or negative zero.No, that's not how it works. 2^31 in hex is 0x7FFF, or in binary, 0111 1111 1111 1111 1111 1111 1111.If you shift left by one bit you get 0xFFFF, or 1111 1111 1111 1111 1111 1111 1111Since the high bit is set (equals 1), this is now a negative number. In
  • #1
YoshiMoshi
228
8

Homework Statement



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.

Homework Equations

The Attempt at a Solution


[/B]
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?
 
Physics news on Phys.org
  • #2
YoshiMoshi said:
So in C an integer is stored as a 32 byte value. Were the 32nd byte is the byte
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?
 
  • Like
Likes berkeman
  • #3
YoshiMoshi said:

Homework Statement



So in C an integer is stored as a 32 byte value. Were the 32nd byte is the byte
Where the 32nd bit is the bit...
YoshiMoshi said:
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.

Homework Equations

The Attempt at a Solution


[/B]
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?
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."
YoshiMoshi said:
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?
 
  • #4
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:
  • #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.
1
10
100
eventually I get to
10000000000000000000000000000000
then one more time through I get
100000000000000000000000000000000
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).
 
  • #6
YoshiMoshi said:
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).
Explained in post #3. For int values, there is no "negative zero."
 
  • #7
(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
 

Related to Understanding Binary Numbers & Homework on Negative Zero

1. What is the purpose of using binary numbers?

The purpose of using binary numbers is to represent and store data in computer systems. Binary numbers are the foundation of how computers process and store information, as they only have two values (0 and 1) which can be easily represented by electronic switches.

2. How do I convert a decimal number to binary?

To convert a decimal number to binary, you can use the repeated division by 2 method. Start by dividing the decimal number by 2 and writing down the remainder. Then, take the quotient and repeat the process until the quotient is 0. The binary number will be the remainders written in reverse order.

3. How do I perform addition and subtraction with binary numbers?

Addition and subtraction with binary numbers follow the same rules as decimal numbers. When adding, if the sum of two digits is 0 or 1, write down the result. If the sum is 2, write down 0 and carry 1 to the next column. When subtracting, if the top digit is smaller than the bottom digit, borrow 1 from the next column and add 2 to the top digit.

4. What is negative zero in binary and how is it represented?

In binary, negative zero can be represented by a string of all 1s or by a 1 followed by all 0s. Negative zero occurs when a negative number is divided by 2, resulting in a fraction that is too small to be represented in binary. It is not commonly used in computer systems as it behaves differently than regular negative numbers.

5. How can I check my work when converting or performing operations with binary numbers?

You can use a binary calculator or an online converter to check your work when working with binary numbers. Another method is to convert the binary number back to decimal and compare it to your original number. Additionally, you can perform the operation in reverse (converting the answer back to decimal) to check for errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Precalculus Mathematics Homework Help
Replies
8
Views
590
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
18K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top