• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
fir

binary difference between working on singed and unsigned

20 posts in this topic

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

 

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
-1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 

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.

1

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

 

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
0

Share this post


Link to post
Share on other sites

 

 

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.

0

Share this post


Link to post
Share on other sites

 

 

 

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
0

Share this post


Link to post
Share on other sites

 

 

 

 

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?

0

Share this post


Link to post
Share on other sites

 

 

 

 

 

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

-1

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites


(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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.


It's worth pointing out that on basically every CPU that has a multiply instruction, it takes two single-register operands and returns a double-register result, so your example isn't all that theoretical - it's the reason why almost every CPU with a multiply instruction has different versions for signed and unsigned.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0