what are major differences between signed and unsigned int
Difference between unsigned int and signed int
Collapse
X
-
Tags: None
-
-
A signed data type uses the first bit to indicate whether it is a positive/negative number. An unsigned data type does not store it’s sign and is usually used for number guaranteed to be positive/negative (such as elapsed time).
An unsigned short (2 bytes) can represent values from 0 to 2^16, while a signed data type can store values form -2^15 to 2^15.
Signed data types can hold both positive and negative values.
Unsigned data types can hold large positive values but cannot hold negative values.
let take an example in c language:
signed int negative=-11; // you can store negative value here//
unsigned int number=11; // here you can only store positive number//Comment
-
A signed data type does not use the first bit to indicate if it is positive or negative, that would be 1's complement, most computers represent signed values as 2's complement.
Using an 8 bit signed integer then
1's complement
1 = 00000001
-1 = 11111110
2's complement
1 = 00000001
-1 = 11111111
In 2's complement you can just do a bit wise addition to add 2 numbers
i.e. 1 + -1 = 00000001 + 11111111 = 00000000 = 0
In 1's complement you have to process first because a bit wise addition of 00000001 and 11111110 would be 11111111 which equals -0.
It is one of the reasons for using 2's complement another being in 1's complement there is 2 representations of 0 00000000, 11111111 i.e. +0 or -0 which also tends to complicate things.
It is true that the MSB does indicate if the value is negative but that is a side effect of the represenation of numbers in 2's compliment rather than the identifying feature of the representation.
The range of a signed (2 byte) value is not -2^15 to 2^15 it is -2^15 to 2^15-1. This can lead to poblems if you are using the limits of the value as it can hold a larger negative number than positive number.Last edited by Banfa; Aug 18 '20, 09:53 AM.Comment
Comment