How does two’s complement work? For example:
5: 00000101
-5 (two's complement): 11111011
How can one tell if the latter is supposed to be 251 or -5?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If its a signed integer, the most significant bit of it, works as its sign bit. If the sign bit is 0 the number is positive. If the sign bit is 1 the number is negative and is presented in 2’s complement form and the compiler does 2’s complement operation to handle these negative integers.
But in the case of unsigned integer declared explicitly as “unsigned int”, there is no sign bit and no 2’s complement interpretation.
So for signed 8 bin int 11111011 is the 2’s complement of 5, hence represents -5, but for unsigned 8 bit int 11111011 is 251 as you mentioned.