|
Example: 8 bits representaion
0 0 0 0 0 1 1 1 ^ | sign Remaining bits is the magnitude (absolute value) bit Examples: 00000111 represents 7 10000111 represents -7 |
![]() |
Examples:
NOTE:
But you only have 3 bits - so the result is: 000
![]() |
Examples:
NOTE:
You have to borrow from an imaginary 1 to the left of the number, i.e.:
the result is: 111
![]() |
Odometer reading: 100 101 110 111 000 001 010 011 ----------------- +----+----+----+----+----+----+----+ Value represented: -4 -3 -2 -1 0 1 2 3 |
Hence, 8 bit 2's complement encoding method represents these values:
Code Value ================ 10000000 -128 <--- smallest negative value with 8 bits (-27) 10000001 -127 ..... 11111000 -8 11111001 -7 11111010 -6 11111011 -5 11111100 -4 11111101 -3 11111110 -2 11111111 -1 00000000 0 00000001 1 00000010 2 00000011 3 00000100 4 00000101 5 00000110 6 00000111 7 00001000 8 ..... 01111111 127 <--- largest positive value with 8 bits (27-1) |
2's compl Value Compare with: Value Binary number system ================ ============================= 10000000 -128 128 10000000 10000001 -127 127 01111111 ..... 11111000 -8 8 00001000 11111001 -7 7 00000111 11111010 -6 6 00000110 11111011 -5 5 00000101 11111100 -4 4 00000100 11111101 -3 3 00000011 11111110 -2 2 00000010 11111111 -1 1 00000001 00000000 0 00000001 1 1 00000001 00000010 2 2 00000010 00000011 3 3 00000011 00000100 4 4 00000100 00000101 5 5 00000101 00000110 6 6 00000110 00000111 7 7 00000111 00001000 8 8 00001000 ..... 01111111 127 127 01111111 |
Notice that:
Example:
00000000 00000001 1 00000010 2 00000011 3 00000100 4 00000101 5 <----- Representation --------------- 00000101 -> 4 + 1 = 5 ^ ^ ^ | | 4 1 |
Example:
11111010 -6 11111011 -5 <----- 11111100 -4 11111101 -3 11111110 -2 11111111 -1 00000000 0 11111011 = representation for -5 + 00000101 = representation for 5 (absolute value of -5) ------------- 100000000 |
These observations will help you understand the conversion procedures below.
Example: v = 7 The value is positive, so: (1) Binary number representation is: 111 (2) 8 digit 2's complement representation is: 00000111 16 digit 2's complement representation is: 0000000000000111 and so on... v = -7 The value is negative, so: (1) Binary number representation for 7 is: 111 (2a) 8 digit 2's complement representation for 7 is: 00000111 (3a) 8 digit 2's complement representation for -7 is: 100000000 - 00000111 ----------- 11111001 (2b) 16 digit 2's complement repr. for 7 is: 0000000000000111 (3b) 8 digit 2's complement repr. for -7 is: 10000000000000000 - 0000000000000111 ----------- 1111111111111001
Example: code c = 00010010 -> it is a positive number the value = 00010010 in binary Convert to decimal: 0 0 0 1 0 0 1 0 16 + 2 = 18 Value = 18 code c = 11101110 -> it is a negative number... (1) Compute: 100000000 - 11101110 ----------- 00010010 (2) the absolute value of the negative value is equal to 00010010 (binary), which is equal to 18 (decimal) (3) Since the value is negative, the value represented by 11101110 is: -18 !
that:
to negate a value, you must subtract the representation by 1000...000
Example: negate 7 100000000 - 00000111 (= 7) ----------- 11111001 (= -7) 100000000 - 00000111 = (1 + 11111111) - 00000111 = 1 + (11111111 - 00000111) [easy subtraction !] = 1 + 11111000Summary, to negate a 2s complement representation:
Example: As you saw above: 00010010 represents +18 To get the representation for -18, you can do this: (1) Flip each bit: 00010010 -> 11101101 (2) Add 1 to result: 11101101 + 1 ---------- 11101110 which is - as you saw above - the representation for -18
Values 8 digit 2's compl repr Adding 2 positive 5 00000101 values + 9 + 00001001 ----- ---------- 14 00001110 -> 8 + 4 + 2 = 14 Adding positive + 5 00000101 negative + -9 + 11110111 ----- ---------- -4 11111100 -> represents -4 Adding negative + -5 11111011 positive + 9 + 00001001 ----- ---------- 4 00000100 -> represents 4 Adding 2 negative -5 11111011 values + -9 + 11110111 ----- ---------- -14 11110010 -> represents -14
Values 8 digit 2's compl repr Subtract 2 positive 5 00000101 values - 9 - 00001001 ----- ---------- -4 11111100 -> represents -4 Subtract positive - 5 00000101 negative - -9 - 11110111 ----- ---------- 14 00001110 -> represents 14 Subtract negative - -5 11111011 positive - 9 - 00001001 ----- ---------- -14 11110010 -> represents -14 Subtract 2 negative -5 11111011 values - -9 - 11110111 ----- ---------- 4 00000100 -> represents 4
For example:
00000011 <---- representation for the value *** (3) + 00000101 <---- representation for the value ***** (5) ------------ 00001000 <---- representation for the value ******** (8)
You can use this program to see why the program prints certain results: click here DEMO
A data type is a certain data representation used in the computer
The various kinds of integer representations (byte, short, int and long) are considered as different data representations
int i; short s; s = 9; <---- s is assigned 0000000000001001 i = s; <---- assign an 16 bit integer to a 32 bit integer (1) 0000000000001001 is sign-extended to: 00000000000000000000000000001001 (2) Then the value is store in variable i s = -9; <---- s is assigned 1111111111110111 i = s; <---- assign an 16 bit integer to a 32 bit integer (1) 1111111111110111 is sign-extended to: 11111111111111111111111111110111 (2) Then the value is store in variable i
int i; short s; i = 9; <---- i is assigned 00000000000000000000000000001001 s = i; <---- assign an 32 bit integer to a 16 bit integer (1) 00000000000000000000000000001001 is truncated to 0000000000001001 (2) Then the value is store in variable s i = -9; <---- i is assigned 11111111111111111111111111110111 s = i; <---- assign an 32 bit integer to a 16 bit integer (1) 11111111111111111111111111110111 is truncated to 1111111111110111 (2) Then the value is store in variable i
i = 90000; i is assigned 00000000000000010101111110010000 s = i; (1) 00000000000000010101111110010000 is truncated to 0101111110010000 (2) assigned to s Problem: s represents 24464, (some bits lost !)
Get the program here: click here DEMO