|
|
|
|
i thought it would convert all the binary digits 0s to 1s |
then displays the maximum number of an integer can hold... |
|
|
|
|
If you want to do it yourself, use unsigned and keep the most significant bit clear |
All bits set is the first negative value, or -1. |
11111111
= -1?
To get the absolute (non-negative) value, we need to manipulate the bits: complement (flip) all the bits and add one. Hence: 11111111 == negative ~1111111 + 1 == negative 0000000 + 1 == negative 0000001 Since an int is a signed value, simply flipping all the bits takes it from all zeros (non-negative zero) to all ones (negative one). |
But 1xxxxxxx means that the value is negative. To get the absolute (non-negative) value, we need to manipulate the bits: complement (flip) all the bits and add one. Hence: 11111111 == negative ~1111111 + 1 == negative 0000000 + 1 == negative 0000001 Since an int is a signed value, simply flipping all the bits takes it from all zeros (non-negative zero) to all ones (negative one). |
11111111 packed is: 1 1111111 1 sign bit and 7 value bits 1111111 toss the sign bit 0000000 flip all bits 0000001 add one result is -1: sign bit was set value bits, properly adjusted, are 1 |
|
|
|
|
|
|
|
|
If you don't follow the math then you won't get the right answer. Take the sign off of a signed number then it becomes unsigned, right? You now have seven bits of value, not eight. 11111111 packed is: 1 1111111 1 sign bit and 7 value bits 1111111 toss the sign bit 0000000 flip all bits 0000001 add one result is -1: sign bit was set value bits, properly adjusted, are 1 Hope this helps. |
|
|
But 1xxxxxxx means that the value is negative. To get the absolute (non-negative) value, we need to manipulate the bits: complement (flip) all the bits and add one. |
|
|
num1 >> 1
is implementation defined, because you're right shifting a negative value. The result of this, then, is whatever your compiler says it should be. There's not any logic or rules you can follow from the standard to derive the correct pattern of bits in this case.
|
|