posted 14 years ago
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 ]
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
leo donahue
Ranch Hand
Posts: 327
posted 14 years ago
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 ]
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 ]
Dirk Schreckmann
Sheriff
Posts: 7023
posted 14 years ago
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
Posts: 327
posted 14 years ago
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.
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.
Thanks, leo
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
