| Author |
Why can't I get it through my head what "add 1" means in binary?
|
leo donahue
Ranch Hand
Joined: Apr 17, 2003
Posts: 327
|
|
I admit, I learn this topic once, then don't use it at all for a while, and then when I come back to it, I can't remember a darn thing. Nothing. What is the Head First way of remembering this? I have int 128 = 10000000 I invert the bits = 01111111 I add one = How in the do I end up with 10000000 again? Will this ever sink in? We could really use a better graemlin for this. [ June 05, 2003: Message edited by: leo donahue ]
|
Thanks, leo
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
|
Remember that in "normal" addition, you always carry to the next column when you reach 10 or more. This is because "normal" addition is base-10. So what number do you think causes a carry in binary arithmetic?
|
Java API Documentation
The Java Tutorial
|
 |
leo donahue
Ranch Hand
Joined: Apr 17, 2003
Posts: 327
|
|
Thanks Layne, I found this web page Hopefully the applet works for everyone. Self, I'm going to remember this...so help me!
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
I have int 128 = 10000000 I invert the bits = 01111111 I add one = ??? How in the ??? do I end up with 10000000 again? leo, Are you maybe confused about figuring out the twos compliment representation of negative 128 using 8 bits? That's what I'm guessing your perplexing about. The first thing to note is that the possible range of values represented by 8 bits in binary using twos comliment is -128 to 127. 00000000 is zero 10000000 is -128 01111111 is 127 To convert from 5 to -5, take the positive representation 00000101 flip the bits 11111010 add one 11111011 To convert from negative to positive, 11111011 flip the bits 00000100 add one 00000101 So, to convert your initial number 10000000 = -128 to a positive value is not possible as the largest representable value is 127, which, when you add one to, the bit pattern wraps around to -128, so 127 + 1 = -128 01111111 +00000001 -------- 10000000 which is -128 So, of course, when we try to negate -128, we are trying to represent the value that is one bigger than 127 (the biggest possible value) and the pattern wraps back around to -128. This wasn't perhaps the clearest explanation, but did we get anywhere? [ June 05, 2003: Message edited by: Dirk Schreckmann ]
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
You also might want to mosey on over to The JavaRanch Campfire and take a gander at the "Cat and Mouse Games with Bits" story.
|
 |
leo donahue
Ranch Hand
Joined: Apr 17, 2003
Posts: 327
|
|
Thanks Dirk, yes, i've mosied through the cat and mouse exercise. I don't know why I always forget how to do this. What happens is that I always try to add a 1 to the visual representation of the binary string instead of adding 1 to the value of the binary string. Probably because the first example I ever saw was an even number in binary and adding a 1 to the visual representation of the binary string made me think I was just adding a 1 to what I was looking at, instead of its value. Does that make any sense? I'm clear on it now, it has a permanent place in memory. And if anyone sees me asking this question again, gouge me with a hot poker.
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8717
|
|
Leo - Sounds like you've got it, but my 'off the cuff' Head First idea is to remember that: 'The left bit is the sign bit'
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
 |
|
|
subject: Why can't I get it through my head what "add 1" means in binary?
|
|
|