问题
Doing my first assignment in Assembler and this is the question posed...
1.Find the doubleword-length 2’s complement representation of each of the following decimal numbers. (Steps of getting your results must be displayed, otherwise you get zero point.)
a -100
b -55555
I have a very foreign professor that I completely can't understand so I'm in need of some assistance. So a doubleword would be 32 bits which would make...
A) 0000|0000|0000|0000|0000|0001|0000|0000
(1
at beginning for negative|0001
for 1|0000
for 0|0000
for 0|leading 0's for filler.
b) 1000|0000|0000|0101|0101|0101|0101|0101 (1
at beginning for negative| 00000000
for filler 0's|the rest 0101
for 5's.
Given that's somehow correct, the 2's complement would be..
I don't know. I learn by examples and my professor didn't do any. If anyone could run through one for me I'd be very grateful. I know there are calculators out there that will convert decimals to their twos compliment but I want to know HOW to do it. Thanks again.
回答1:
Way off track. Remember they were decimal numbers, you can't just suddenly pretend they're hexadecimal (and use nibbles), and "just putting a 1 at the beginning" is about as far removed from two's complement as you can be.
So let's begin at the start, convert 100 and 55555 to binary. Subtract the highest power of two that will go into it, set the corresponding bit to 1. So for 100:
100 - 64 = 36, set bit 1000000
36 - 32 = 4, set bit 100000
4 - 4 = 0, set bit 100
------- +
1100100
Or 0x64
in hexadecimal. 55555 is just more of the same.
You've probably learned (or should have learned) the identity -x = ~x + 1
, using that it's simple to negate 100 and 55555. Take the binary representation, as wide as you want the result to be (32 bits), so
0000 0000 0000 0000 0000 0000 0110 0100
Invert all the bits:
1111 1111 1111 1111 1111 1111 1001 1011
Add 1:
1111 1111 1111 1111 1111 1111 1001 1100
Or 0xFFFFFF9C
in hexadecimal.
回答2:
I SO wish that someone had explained this to me in simpler terms a long time ago.
How the "Two's Complement" Thing Works...
Step One: Turn the number into a binary string of 1 and 0 bits
Step Two: Flip all those bits
Step Three: Add one to that result
Two quick examples, in 8-bit integers
Decimal Number: 37
Make it Hex: 025h
Make it Binary: 0010 0101
Flip those bits: 1101 1010
Add binary one: 0000 0001
Get this result: 1101 1011
That is the 8 bit representation of the integer, negative 37 (decimal)
Example, the other way
Decimal Number: -41 (that's negative 41)
Make it Hex: 0D7h
Make it Binary: 1101 0111
Flip those bits: 0010 1000
Add binary one: 0000 0001
Get this result: 0010 1001
That is the 8 bit representation of the integer, 41 decimal (i.e., positive 41)
Incidentally, as an exercise, you can hit the same value with two's compliment all day along and the value with just flip back and forth between the negative and positive number.
As for 16 bit, 32 bit, and 64 bit numbers, you just extend it to the higher bits.
来源:https://stackoverflow.com/questions/25749653/doubleword-length-2-s-complement-representation-of-binary-number