Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is two's complement notation of a positive number the same number?

Is two's complement notation of a positive number is same as its binary representation?

like image 768
yesraaj Avatar asked Apr 27 '09 15:04

yesraaj


2 Answers

Some of the answers and comments are getting the relationship between a "two's complement notation" and the "two's complement of a number" confused. The question may need to be clarified a bit, but it is clearly asking about "two's complement notation."

Two's complement notation includes both positive and negative numbers. Binary numbers can mean lots of things, so in order to determine what any binary number is supposed to represent, one must first know what notation or encoding is being used. The binary number could be an unsigned integer, two's complement integer, an IEEE floating point number, a string of characters, or something else entirely.

So 7 in two's complement notation is 00000111, just as it is as an unsigned integer. And -7 in two's complement notation is 11111001.

So, yes, positive integers in two's complement notation are represented the same way they are with unsigned integers (assuming it is a valid integer for the number of bits being used).

like image 73
Rob Pilkington Avatar answered Oct 22 '22 17:10

Rob Pilkington


I think that you are confusing something here. Positive integers are generally stored as simple binary numbers. 1 is 1, 10 is 2, 11 is 3, etc.. Negative integers are stored as the two's complement of their absolute value, i.e. of the corresponding positive integer. The two's complement of a positive number is, when using this notation, a negative number.

In order to flip the sign of a number, you always calculate the two's complement of that number: flip all bits, then add 1. This is independent of whether the original number is positive or negative.

Example: 3 in 8-bit signed binary notation is 00000011. To flip the sign, you first flip all bits (11111100), then add 1 (11111101). So, -3 is 11111101. To flip the sign again, you first flip all bits (00000010), then add 1 (00000011), and you can see that this is the same 3.

like image 28
Svante Avatar answered Oct 22 '22 16:10

Svante