math wiz: 2's complement

Bluga

Banned
Nov 28, 2000
4,315
0
0
Can someone explain this?

Express the following numbers in the 2's complement using 8 bits.

-1=====>11111111

150====> impossible
 

Capn

Platinum Member
Jun 27, 2000
2,716
0
0
150====>

10010110 unless I'm doing something wrong, why do you say it's impossible

*ok you changed the problem it seems, never heard of something regarded as 2's complement

** Looked up two's complement, seems it's only for binary notation of negative numbers, so I don't understand the problem
 

propellerhead

Golden Member
Apr 25, 2001
1,160
0
0
You can't express 150 in two's complement because it requires a sign bit. You are left with 7 bits for the magnitude.
 

DanceMan

Senior member
Jan 26, 2001
474
0
0
Yep, it's done so that you can express negative numbers using binary. And since you need a sign bit, you only get n-1 bits left out of a byte to represent a number.

Two's complement is done so that regular arithmetic operations of a CPU (addition) can still be done without having to come up with some fancy scheme for representing negative numbers. In fact, some CPU's don't implement a direct subtract, they just take the two-complement of the 2nd operand and add.

DanceMan
 

ChinamanatNCSU

Golden Member
Nov 15, 2001
1,160
0
0


<< You can't express 150 in two's complement because it requires a sign bit. You are left with 7 bits for the magnitude. >>



what he said. in 2's complement the far left bit is the sign bit. 1 = negative, then which you flip all the bits and add 1 to get the value. 0 = positive, and you find the decimal value the same way as any other binary number. so, 150 is imposibble b/c with 8 bits the biggest positive number you can have is 01111111, or 127, and the largest negative number is 10000000.
 

DanceMan

Senior member
Jan 26, 2001
474
0
0
Well, if you haven't done the net search yet (and I hope I'm not doing your homework)...

If I remember correctly, you invert the bits as if you didn't have the negative in front of it, and then add 1 to the result.

For example, you -1 result from above:

Two's complement ABS(-1) = 1 = 00000001 (in 8-bit representation)
Then invert bits ~(00000001) = 11111110 (in 8-bit representation)
Then add 1: 11111110 + 00000001 = 11111111 (this is the number in two's complement above)

Hope this helps...

DanceMan
 

EvilYoda

Lifer
Apr 1, 2001
21,200
9
81
AHHHHHHHHHH! NOOOOO!!! I'm on spring break, I thought I was done with 206 for at least a week. ;) But yeah, what people have said is correct, the first (leftmost) bit is the sign-bit, only leaving you with 7 bits to do your work, leaving you with the highest possible number of 127 (I think?). Just incase you're still unclear, 2's complement is 1's complement (switching all the 0's to 1's and 1's to 0's) and using binary addition, adding a '1', i.e. 01111111, 2's complement is 10000000 + 00000001 = 10000001.

Back to not thinking. darn you. ;)
 

Bluga

Banned
Nov 28, 2000
4,315
0
0

ok so:

For normal numbers like 34, 120, 145...etc, i keep dividing 2 and read the remainder from bottom to top right?

For signed number like -1, -49, take ABS(), then invert bits, then add 1right?

If i want to do the opposite, ie 2's compliment to decimal, just reverse steps above right?
 

Mday

Lifer
Oct 14, 1999
18,646
1
76
dood if you cant handle this, wait unti l you do floating point (which is not much harder)
 

tops2

Senior member
Oct 6, 2000
711
0
0
for 8 bits 2's complement, isn't the highest 127 and the lowest is -127?

<dood if you cant handle this, wait unti l you do floating point (which is not much harder) >
yup, doing floating point its a bit more messy, but if ur careful, its not too bad...kinda fun after a while...
;-)