binary difference between working on singed and unsigned

Started by
19 comments, last by Anthony Serrano 10 years, 1 month ago

I found an interesting piece of text

Signed and Unsigned Variables[edit]

Integer formatted variables, such as int, char, short and longmay be declared signed or unsigned variables in the C source code. There are two differences in how these variables are treated:

  1. Signed variables use signed instructions such as add, and sub. Unsigned variables use unsigned arithmetic instructions such as addi, and subi.
  2. Signed variables use signed branch instructions such asjge and jl. Unsigned variables use unsigned branch instructions such as jae, and jb.

The difference between signed and unsigned instructions is the conditions under which the various flags for greater-then or less-then (overflow flags) are set. The integer result values are exactly the same for both signed and unsigned data.

(this is from http://en.wikibooks.org/wiki/X86_Disassembly/Variables )

Myself was never conscious of binary difference results of working on signed instead of unsigned and vice versa

Does it mean that all binary stays the same (if you switch s. to u. or u. to s.)the only difference will be in some branching (comparisons?) (or some other results can also change, which one?)

Advertisement

Take any two random binary patterns and add them, and you get the same binary pattern as a result whether you treat the operands and the result as two's complement signed or unsigned integers. Thus, the physical adder hardware itself does not have to consider two's complement signed or unsigned integers.; the process of adding the binary patters are the same.

What changes is flags used for conditional operations. For example, assuming 32-bit integers, 0x7FFFFFFF + 0x00000001 overflows with signed addition but not with unsigned addition, so the overflow-flag has to be set if the values are treated as signed.

That's what two's complement has as an advantage over other binary representations, such as one's complement.

Take any two random binary patterns and add them, and you get the same binary pattern as a result whether you treat the operands and the result as two's complement signed or unsigned integers. Thus, the physical adder hardware itself does not have to consider two's complement signed or unsigned integers.; the process of adding the binary patters are the same.

What changes is flags used for conditional operations. For example, assuming 32-bit integers, 0x7FFFFFFF + 0x00000001 overflows with signed addition but not with unsigned addition, so the overflow-flag has to be set if the values are treated as signed.

That's what two's complement has as an advantage over other binary representations, such as one's complement.

adding stays the same maybe

but multiplying and divisions for sure not :/

not sure as to substracting

for a while i was thinking that there are no binary differences but

it seems that there are many

Signed and unsigned subtraction and multiplications are identical as well. Not sure about division though.

Just checked division, and it appears to be identical for signed two's complement and unsigned as well.

edit: see my post below about negative numbers for correction.

Signed and unsigned subtraction and multiplications are identical as well. Not sure about division though.

you sure?

-2 * -2 should be 4

is the 0xff ff ff fe * 0xff ff ff fe == 0x00 00 00 04 ?

(well i do it in the calc.exe and it bring FFFF FFFC 0000 0004

: O

does it mean that in general arythmetic is all binary the same?

a bit strange

specifically does it mean that all the c arithmetic could be

done on siged only or unsigned only and nothing wil change

(as c arthimetic does not use a flags probably and is doing

only explicit test like x>-239 which should work always regardles as considering signed or unsigned only binary patterns needed) ?

Yes, as I said, that's the benefit of using two's complement. From a hardware perspective, the binary result is the same.

I have to correct myself about division though, I ended up not trying negative numbers and division is different when at least one operand is negative.

I have to correct myself about division though, I ended up not trying negative numbers and division is different when at least one operand is negative.

So is multiplication.

     1011  (unsigned 11d, signed -5d)
   X 0111  (unsigned 7d, signed 7d)
   ======
     1011
    1011
   1011
+ 0000
=========
  1001101  (unsigned 77d, signed -51d)
I'm not sure what multiplication algorithms or circuits are used these days, but I don't think there is one general circuit that can handle signed and unsigned values.

You'd have to use two's complement on the signed value before multiplying:
     0101  (two's complement of 1011, i.e. -5d)
   x 0111  (unsigned 7d)
   ======
     0101
    0101
   0101
+ 0000
=========
   100011  (unsigned 35)
And then do the necessary corrections to convert the result into signed format again (because -5 * 7 is usually -35)
"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

I have to correct myself about division though, I ended up not trying negative numbers and division is different when at least one operand is negative.

So is multiplication.


     1011  (unsigned 11d, signed -5d)
   X 0111  (unsigned 7d, signed 7d)
   ======
     1011
    1011
   1011
+ 0000
=========
  1001101  (unsigned 77d, signed -51d)
I'm not sure what multiplication algorithms or circuits are used these days, but I don't think there is one general circuit that can handle signed and unsigned values.

You'd have to use two's complement on the signed value before multiplying:

     0101  (two's complement of 1011, i.e. -5d)
   x 0111  (unsigned 7d)
   ======
     0101
    0101
   0101
+ 0000
=========
   100011  (unsigned 35)
And then do the necessary corrections to convert the result into signed format again (because -5 * 7 is usually -35)

When you truncate the values to 4 bits to match the format of the inputs and negate the second product, the binary patterns are the same.

When you truncate the values to 4 bits to match the format of the inputs and negate the second product, the binary patterns are the same.

Can you help me out here with an example? I don't understand what you mean.
"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

This topic is closed to new replies.

Advertisement