[Home]Twos Compliment

HomePage | RecentChanges | Preferences

No diff available--this is the first major revision. (no other diffs)

2's compliment numbers

Two compliment numbers are used in computers to represent negative numbers in unsigned numbers. They are modulo numbers. They form a ring of numbers.

I want 2's compliment numbers for bases other than powers of 2

I want two compliment and modulo arithmetic for base 100. What do I have to do to adjust a value in an 8 bit register.

Here is the number line:

  + - -+ - -+ - -+ - -+ - -+ - -+
  -3   -2   -1   0    1    2    3

  253  254  255  0    1    2    3

  97   98   99   0    1    2    3

Lets add -1 to -1 to get -2.

When 2^8 =256, 255 represents -1 Lets add 255 to 255.

I get 510, getting an overflow and 510-256 = 254 which represents -2

Lets add 99 to 99. I get 198, but 99 is also -1 in 2's compliment where 2^n = 100 ( n is not an integer )

If result is > 100 subtract 100 to give 98 which represents -2.

Now lets add -10 to -10 to get -20 in 2's compliment.

For 2^8 = 256

 256 - 10 = 246

 256 - 10 = 246

 246 + 246 = 492

 492-256 = 236

 236 - 256 = -20

For 2^n = 100 storing in 8 bits

 100 -20 = 80
 100 -20 = 80

 80 + 80 = 160

 If 160 > 100 then 160 - 100
 = 60

 if 60 > 100 / 2 then 60 - 100 = - 40

For 2^n = 200 storing in 8 bits

 200 -20 = 180
 200 -20 = 180

 180 + 180 = 360 which does not fit in 256 

 180 + 180 = 360 modulo 256 = 104  

 If 104 > 100 then 160 - 100
 = 60

 if 104 > 100 / 2 then 60 - 100 = - 40

2's compliment numbers - rules

Numbers range from 0 to N where N is a power of 2

To save negative numbers add a constant K so that the numbers are between 0 and N

Do arithmetic in registers that can store 0 to 2N

Ensure that the results are constrained between 0 and N

convert back to negative by subtracting K

N does not need to be a power of two but you need to use registers that store unsigned numbers greater that 2*N.

2's compliment numbers - skewed Number lines.

  + - -+ - -+ - -+ - -+ - -+ - -+ - -+
  -2   -1   0    1    2    3    4    5

  6    7    0    1    2    3    4    5

 K = 2

 2^n=8


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited 2026-08-20 7:33 am by dougrice.plus.com (diff)
Search:
home page