PDA

View Full Version : Understanding Integer types



xGTx
31-05-2005, 12:02 AM
If any of you have the time too answer a few questions, it'd fill a gap in my head! :)

I'm trying to understand Integer types. What is the difference between signed (eg. integer, shortint, smallint, longint, int64), and unsigned (eg. cardinal, byte, word, longword)?

Why can a 16-bit Word hold twice+1 as much as a 16-bit smallint?

I do realize that unsigned integer types can only be 0..whatever and not negative, how does a computer know if an integer is negative at binary level? My idea is that the first bit tells if it's positive or negative, thus explaining why unsigned integer types hold *2+1.

Thanks in advance!

WILL
31-05-2005, 03:02 AM
The whole difference is that they are signed.

Every data-type has a number of bits that it uses to hold the value, however signed variables use one of those bits to indicate either possitive or negative. Hence it's a signed value being stored.

To completely understand this you have to know at least the basics of binary data.

Like the decimal system is set to the base of 10, the binary system is set to the base of 2. Decimal system has 10 characters. Binary has just 2. See how that works?

So instead of Ones, Tens, Hundreds, Thousands and Ten Thousands places. You have Ones, Twos, Fours, Eights, Sixteens, Thirty Twos, etc.

ie.

1 in binary is 1 in decimal
10 in binary is 2 in decimal
100 in binary is 4 in decimal
1000 in binary is 8 in decimal
10000 in binary is 16 in decimal

and so on...

Now lets say we have an 8 bit un-signed varible...

If 11111111 is the value then, in decimal it's 255.

To figure this out yourself add each binary place like you can do wit hthe decimal system.

Here is the math:

1+2+4+8+16+32+64+128=255

Now, for a signed 8-bit value... don't count the top bit. We're gonna use it to tell if it's + or - 1 is negative and 0 is possitive.

0 1111111 in binary is 127 in decimal.

Here is the math:

1+2+4+8+16+32+64=127

Now take 1 1111111 and in decimal it'll be -127. Simple no?


One last thing as an interesting observation; Notice how each extra bit used to store your variable enables you to double your potential highest value into it? Well in the decimal system it's the same too. Add a digit and you have 10 times the potential value that can be stored.


I hope that should clear up some confusion and hopefully give you a better undertanding.

xGTx
31-05-2005, 03:51 AM
That was exactly how I thought of it, and im just glad I had such a nice guy verify it for me :) (I actually didn't think about it until I was writing the post, but it all made sense to me, but still for verification i hit "Post" :) )

Thank you!

{MSX}
31-05-2005, 06:55 AM
Well as far as i know that's not the way negative values are stored.

They are stored in the so called "twos-complement representation".
That is, a number has all bit flipped and 1 added.


For example, in a 16 bit integer, the number 19 is written

0000000000010011

Flipping the bits produces

1111111111101100

and finally, adding one gives

1111111111101101

Notice that this holds no matter how many bits are being used to represent the integer: If it were a 32 bit integer, we would simply have an additional 16 ones added to the left.

The reason of the two-complement representation is that it allows you to sum 2 signed integers without caring for the sign. With the method you described, it's necessary to test the sign bit before proceeding with sum, and to behave differently.


For example, imagine that you want to add the number 32 (0000000000100000) to -19:

0000000000100000
+1111111111101101
__________________
10000000000001101


This gives us the correct answer of 13


A detailed description is here:

http://class.et.byu.edu/ce571/notes/binary.html

bye

Turbo_Pascal
08-06-2005, 04:47 PM
>They are stored in the so called "twos-complement representation".

that's right, in Pascal/Delphi at least that is the way used for store negatives values,

var
a:shortint;
begin
a:=1; //00000001
a:=-1; //11111111
end;


However keep in mind that there is not way you know if a value is negative or positive cheking his bits, for example a signed byte type just mean 8 bits are used for INTERPRET vales from -128 to 127 and a unsigned byte mean same 8 bits are used for INTERPRET values from 0 to 255,

var
a:shortint
b:byte;

a:=-1; // 11111111
b:=255; // 11111111 same as -1 when signed!

The compiler resolve how interpret values (if signed or unsigned) in compiling time.


tp.