How are negative numbers represented in 32-bit signed integer?
I think the answer is 0110
, preceeded by 1 repeated 28 times, therefore it looks like:
1111 1111 1111 1111 1111 1111 1111 0110;
Steps:
bit representation for 10 is:
0000 0000 0000 0000 0000 0000 0000 1010;
0->1
and1->0
for all the bits:1111 1111 1111 1111 1111 1111 1111 0101;
add 1 to the last bit, and propagate to the bit ahead, done!
1111 1111 1111 1111 1111 1111 1111 0110;
===
You can verify by adding it with 10, and you will get 0 for all bits. As mentioned above, it is 2-based and follows two's complement.
Most computers these days use two's complement for signed integers, but it can vary by hardware architecture, programming language, or other platform-specific issues.
For a two's-complement representation, the most-significant ("leftmost") bit is referred to as the sign bit, and it will be set for a negative integer and clear for a non-negative integer. However, it is more than just a "flag". See the Wikipedia article for more information.