Jump to content

  • Log In with Google      Sign In   
  • Create Account


binary difference between working on singed and unsigned


Old topic!
Bing, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 fir   Members   -  Reputation: -444

Like
0Likes
Like

Posted 28 February 2014 - 04:09 AM

I found an interesting piece of text

 

Signed and Unsigned Variables[edit]

Integer formatted variables, such as intcharshort 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?)

 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7781

Like
3Likes
Like

Posted 28 February 2014 - 04:24 AM

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.



#3 fir   Members   -  Reputation: -444

Like
0Likes
Like

Posted 28 February 2014 - 04:32 AM

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



#4 Brother Bob   Moderators   -  Reputation: 7781

Like
1Likes
Like

Posted 28 February 2014 - 05:16 AM

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



#5 Brother Bob   Moderators   -  Reputation: 7781

Like
0Likes
Like

Posted 28 February 2014 - 05:28 AM

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.


Edited by Brother Bob, 28 February 2014 - 05:45 AM.


#6 fir   Members   -  Reputation: -444

Like
-1Likes
Like

Posted 28 February 2014 - 05:31 AM

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) ?


Edited by fir, 28 February 2014 - 05:40 AM.


#7 Brother Bob   Moderators   -  Reputation: 7781

Like
0Likes
Like

Posted 28 February 2014 - 05:44 AM

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.



#8 TheComet   Members   -  Reputation: 1385

Like
0Likes
Like

Posted 28 February 2014 - 06:00 AM

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)

Edited by TheComet, 28 February 2014 - 06:05 AM.

YOUR_OPINION >/dev/null


#9 Brother Bob   Moderators   -  Reputation: 7781

Like
1Likes
Like

Posted 28 February 2014 - 07:09 AM

 

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.



#10 TheComet   Members   -  Reputation: 1385

Like
0Likes
Like

Posted 28 February 2014 - 07:20 AM

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.

YOUR_OPINION >/dev/null


#11 fir   Members   -  Reputation: -444

Like
0Likes
Like

Posted 28 February 2014 - 07:26 AM

 

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.

 

 

you need tu truncate output to the size of the input fields (bytes)

- here 11x7 you will get overflow but if extended the example to whole byte you will get correct result for both ++ and +- or probably -- to

 

           1011  //11
        X 0111  //; 7

= 01001101  // = 77

 

             1011  //-5
          X 0111  //; 7

=   11011101 //-35

 

 
the part of input field are correct
 
i was asking mainy about c 'flagless' arthimetic where
i suppose (not sure where i got overflow i get tle less
meaning bits so on this portion of data i will get right
part of results
 
sadly it probably dos not working for divisions
(and at all i dont know what to thing about it all, what conclusions to get)

Edited by fir, 28 February 2014 - 07:37 AM.


#12 TheComet   Members   -  Reputation: 1385

Like
0Likes
Like

Posted 28 February 2014 - 07:29 AM

 

 

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.

 

 
you need tu truncate output to the size of the input fields (bytes)
- here 11x7 you will get overflow but if extended the example to whole byte you will get correct result for both ++ and +- or probably -- to

Yes but since this is a theoretical example, there is no such thing as "overflow", I can add as many bits as I theoretically need.


YOUR_OPINION >/dev/null


#13 fir   Members   -  Reputation: -444

Like
0Likes
Like

Posted 28 February 2014 - 07:43 AM

 

 

 

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.

 

 
you need tu truncate output to the size of the input fields (bytes)
- here 11x7 you will get overflow but if extended the example to whole byte you will get correct result for both ++ and +- or probably -- to

Yes but since this is a theoretical example, there is no such thing as "overflow", I can add as many bits as I theoretically need.

 

dont quite understand what you mean but really you did a mistake in your example imo, becouse you must work on the whole filelds if we talkin about 4bit -5 x 7  is  1011 x 0111 but if we speaking about 8 bits -5 x 7 is 11111011 x 00000111 [not as you wrote it 00001011 x 00000111]

 

 

so the conclusion may be that if i wrote funtions

 

unsigned add(unsigned a, unsinged b) {return a+b;}

unsigned sub(unsigned a, unsinged b) {return a-b;}

 

unsigned mul(unsigned a, unsinged b) {return a*b;}

 

they will be probably working also for ints and vice versa

but probably

 

unsigned div(unsigned a, unsinged b) {return a/b;}

and

int div(int a, int b) {return a/b;}

 

are not binary compatible (i am not sure though)

 

 

 

 

 

 

 

 

 


Edited by fir, 28 February 2014 - 07:55 AM.


#14 TheComet   Members   -  Reputation: 1385

Like
0Likes
Like

Posted 28 February 2014 - 08:04 AM

 

 

 

 

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.

 

 
you need tu truncate output to the size of the input fields (bytes)
- here 11x7 you will get overflow but if extended the example to whole byte you will get correct result for both ++ and +- or probably -- to

Yes but since this is a theoretical example, there is no such thing as "overflow", I can add as many bits as I theoretically need.

 

dont quite understand what you mean but really you did a mistake in your example imo, becouse you must work on the whole filelds if we talkin about 4bit -5 x 7  is  1011 x 0111 but if we speaking about 8 bits -5 x 7 is 11111011 x 00000111 [not as you wrote it 00001011 x 00000111]

 

 

so the conclusion may be that if i wrote funtions

 

unsigned add(unsigned a, unsinged b) {return a+b;}

unsigned sub(unsigned a, unsinged b) {return a-b;}

 

unsigned mul(unsigned a, unsinged b) {return a*b;}

 

they will be probably working also for ints and vice versa

but probably

 

unsigned div(unsigned a, unsinged b) {return a/b;}

and

int div(int a, int b) {return a/b;}

 

are not binary compatible (i am not sure though)

Oooh, that makes sense. OK, I get it.

 

But this here:

    11111011  (signed -5)
  x 00000111  (signed 7)
  ==========
    11111011
   11111011
+ 11111011
============
 11011011101
=>  11011101 (signed -35)

Is still a different operation than this:

    00001011  (unsigned 11)
  x 00000111  (unsigned 7)
  ==========
    00001011
   00001011
+ 00001011
============
     1001101
=>  01001101 (unsigned 77)

Is it not?


YOUR_OPINION >/dev/null


#15 fir   Members   -  Reputation: -444

Like
-1Likes
Like

Posted 28 February 2014 - 08:10 AM

 

 

 

 

 

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.

 

 
you need tu truncate output to the size of the input fields (bytes)
- here 11x7 you will get overflow but if extended the example to whole byte you will get correct result for both ++ and +- or probably -- to

Yes but since this is a theoretical example, there is no such thing as "overflow", I can add as many bits as I theoretically need.

 

dont quite understand what you mean but really you did a mistake in your example imo, becouse you must work on the whole filelds if we talkin about 4bit -5 x 7  is  1011 x 0111 but if we speaking about 8 bits -5 x 7 is 11111011 x 00000111 [not as you wrote it 00001011 x 00000111]

 

 

so the conclusion may be that if i wrote funtions

 

unsigned add(unsigned a, unsinged b) {return a+b;}

unsigned sub(unsigned a, unsinged b) {return a-b;}

 

unsigned mul(unsigned a, unsinged b) {return a*b;}

 

they will be probably working also for ints and vice versa

but probably

 

unsigned div(unsigned a, unsinged b) {return a/b;}

and

int div(int a, int b) {return a/b;}

 

are not binary compatible (i am not sure though)

Oooh, that makes sense. OK, I get it.

 

But this here:

    11111011  (signed -5)
  x 00000111  (signed 7)
  ==========
    11111011
   11111011
+ 11111011
============
 11011011101
=>  11011101 (signed -35)

Is still a different operation than this:

    00001011  (unsigned 11)
  x 00000111  (unsigned 7)
  ==========
    00001011
   00001011
+ 00001011
============
     1001101
=>  01001101 (unsigned 77)

Is it not?

 

 

why -5 x 7 would be the same to 11 x 7? -5 is 251 on 8 bits



#16 TheComet   Members   -  Reputation: 1385

Like
0Likes
Like

Posted 28 February 2014 - 08:21 AM

It appears I was not thinking straight, never mind.


YOUR_OPINION >/dev/null


#17 Vortez   Crossbones+   -  Reputation: 2688

Like
2Likes
Like

Posted 28 February 2014 - 08:29 AM

At this point why not just look up some articles on the net:

 

http://www.cs.utexas.edu/users/chris/cs310/current/notes/DataOperations_ppt4in1.pdf

http://embeddedgurus.com/stack-overflow/2009/05/signed-versus-unsigned-integers/

http://stackoverflow.com/questions/2084949/arithmetic-operations-on-unsigned-and-signed-integers

 

ect...

 

(and do you really have to quote 10 posts to add 1 line of comment fir??? It not like the conversation is hard to follow here...)


Edited by Vortez, 28 February 2014 - 08:31 AM.


#18 jHaskell   Members   -  Reputation: 955

Like
0Likes
Like

Posted 28 February 2014 - 02:03 PM


(and do you really have to quote 10 posts to add 1 line of comment fir??? It not like the conversation is hard to follow here...)

 

It is when your quotes go ten levels deep.

 

More on topic, when performing a multiply or divide, it's important to keep in mind that you have to perform the appropriate sign extension of the intermediate results based on the sign of the two values, and not just automatically assume 0 will be the extension.



#19 Bregma   Crossbones+   -  Reputation: 4765

Like
0Likes
Like

Posted 28 February 2014 - 02:42 PM

Part of the obfuscation here is the confabulation between the radix and the numeral system.  The difference between unsigned binary and two's-complement binary is like the difference between octal and decimal.  The string of digits '127' has different meanings in octal and decimal, just like the string of digits '1101' has different meanings in unsigned binary and in two's-complement binary.  Sure, they're all compatible, just as they're all really shorthand notation for the sum of polynomials in a given radix.  What they're not is interchangeable.

 

Things that look alike are not necessarily alike in other ways.

 

For a really good understanding, read the chapter in Knuth's TAOCP about biased base 3.  We should be using that for all our numbers.


Stephen M. Webb
Professional Free Software Developer

#20 Álvaro   Crossbones+   -  Reputation: 11891

Like
1Likes
Like

Posted 28 February 2014 - 03:01 PM

Another way to look at why unsigned and two's-complement integers behave the same under addition, subtraction and multiplication is that they are both different ways of thinking of the commutative ring Z/(2^32). The definition of that ring says that its elements are the classes of equivalence in Z (the integers) when two numbers are considered equivalent if their difference is a multiple of 2^32. This is a ring with 2^32 elements, and you can use {0, 1, 2, ..., 2^32-1} as representatives or {-2^31, -2^31+1, ..., 2^31-1} as representatives. Two's complement is the way of looking at this ring by picking the latter representatives. But you are performing the exact same operations.

 

Things like overflow and order are concepts that are not part of the Z/(2^32) structure, so you have differences in what those mean between signed and unsigned.


Edited by Álvaro, 28 February 2014 - 03:02 PM.





Old topic!
Bing, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS