This is a doubt regarding the representation of a signed integer bit. For example, if you want to represent -1, this is equivalent to 2 additions (+1). So -1 is represented as 0xFFFFFFF. Now, when I change my number to 31 and print the result, it returns as -1.
signed int a = -1; printf(("The number is %d ",(a>>31));//this prints as -1
So can someone explain to me how bits are represented for negative numbers?
Thanks.
When the top bit is zero, the number is positive. When it is 1, the number is negative.
Negative numbers are shifted to the right, keeping the offset "1" as the top bit to preserve the negative number. That is why you get the answer.
. fooobar.com/questions/40870/....
@Stobor , C 0 1. [ .] Java .
, , , .
C undefined , ( ) ( ) ( ) . .
, , . ( , ), , , .
, .
. . , , 0. , , . ( ), - .
" โ " , , , , , , , . , , , .
. .
EDIT: , :
unsigned int a = -1; printf(("The number is %d ",(a>>31));//this prints as -1
unsigned int 32 , -1 ( , unsigned int , Printf).
a - unsigned int, -1 UINT_MAX ( , -1 UINT_MAX + 1). unsigned int 32 , 31 UINT_MAX, 2 ^ 31, int. ( unsigned int 31 , , , ).