3

I'm aware of the two complement representation. I was wondering what are the specifics differences, in terms of implementation between int and unsigned int. I would than say that

  1. Comparison is different (the sign bit will change how the comparison is performed).
  2. Multiplication is different (I take the modulus, multiply such modules and complement the result based on the sign of both operands).
  3. Division is different (same reason of multiplication).
  4. Addition and subtraction look the same

Are there any other differences that maybe I'm not aware of?

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • 1
    Since you tagged C++ specifically, `unsigned int` "wrap around", but for `signed int`s overflowing is Undefined Behaviour. – BoBTFish Jan 23 '17 at 10:43
  • i think the only difference between them is , `int` is twos complement , and `unsigned int` is not twos complement , so `2^31` in `int` is a negative number but `2^31` in `unsigned int` is a positive number , for powers less than 31 in `int` and `unsigned int` all of bits are positive – Mohsen_Fatemi Jan 23 '17 at 10:43
  • Signed int is used for representation negative and positive numbers, so that value range for signed int: -2147483648 to 2147483647 for unsigned: 0 to 4294967295. Signed integer uses two's complement for negative values. For example 8-bit signed value (-1) represents as 1111 1111. The next rules are used for negative value representation: 000 0001( invert ) -> 1111 1110( plus one )-> 1111 1111 – arturx64 Jan 23 '17 at 10:46
  • look at it [here](http://stackoverflow.com/questions/3812022/what-is-a-difference-between-unsigned-int-and-signed-int-in-c) and [here](http://stackoverflow.com/questions/5739888/what-is-the-difference-between-signed-and-unsigned-int) – Mohsen_Fatemi Jan 23 '17 at 10:47
  • Possible duplicate of [What is the difference between signed and unsigned int](http://stackoverflow.com/questions/5739888/what-is-the-difference-between-signed-and-unsigned-int) – Mohsen_Fatemi Jan 23 '17 at 10:48
  • Twos complement is also - despite claims by some otherwise - not the only way of implementing signed integers. Existing alternatives include ones-complement, signed magnitude. – Peter Jan 23 '17 at 10:52
  • @Mohsen_Fatemi All your links and remarks are in regards of `C`. OP tagged `C++` specifically. You can't just assume the behaviour to be the same. – Simon Kraemer Jan 23 '17 at 10:53
  • @Mohsen_Fatemi AFAIK: Signed integer is not specified to must use a specific representation in `C++`. – Simon Kraemer Jan 23 '17 at 10:57
  • @SimonKraemer yeah , you are right , [here](http://www.cplusplus.com/forum/beginner/750/) – Mohsen_Fatemi Jan 23 '17 at 10:57
  • Just to clarify guys, I'm not asking the difference in "representation", but in "implementation". For example x - y would be the same for both signed and unsigned representation, the interpreted result would be different (underflow in int representation, and negative number in signed representation but the operation would be exactly the same). – user8469759 Jan 23 '17 at 10:59

1 Answers1

0

I'm assuming two-complement arithmetic as this is the most common.

There are plenty of explanations of twos complement arithmetic out there. For example, links in the comments, and here for multiplication: http://pages.cs.wisc.edu/~smoler/cs354/beyond354/int.mult.html

1: Correct. Comparison is typically implemented the same as subtraction - but the results of the subtraction are discarded and only the status bits are used. Nit-pick: "<" and ">" are different, but "==" and "!=" are the same.

2, 3: Yes, multiplication and division are different.

4: Well, sort of. The bit-pattern of the result is the same, but there are important differences. The add/sub instructions on a typical processor set status flags for overflow, carry, negative and zero. So the difference I suppose is how you interpret the results rather than the results themselves. These status bits are not available to a C/C++ program, but are used by the code generated by the compiler.

5: Extension. Casting to a wider type is different. For unsigned integrals, they are "zero extended", while for signed integrals they are "sign extended". Sign extension means that it will copy the high order bit (the sign bit) of the narrow type to fill in the additional bits of the wide type.

6: Range: For example, the range of values for an unsigned 8-bit is 0...255, while for a signed 8-bit value it is -128...+127.

7: bit-wise operations "&", "|", "~", and "^" are the same

8: bit-shift operations "<<" and ">>": Left shift is the same, but right-shift is different as signed values right shifted do sign extension.

Community
  • 1
  • 1
joeking
  • 2,006
  • 18
  • 29