teach-ict.com logo

THE education site for computer science and ICT

3. Signed integer

Quite often, the software needs to handle both positive and negative whole numbers such as +27 and -20123. In which case unsigned integer cannot be used.

Just like unsigned integer a signed integer can be long (4 bytes) or short (2 bytes).

There are two approaches to handling negative integer numbers. The first is 'sign and magnitude' and the other, more popular method is '2s compliment'.

The range of positive numbers a signed integer can represent is halved compared to unsigned because it has to include negative numbers.

For a signed integer, the leftmost bit indicates the sign of the number.

For a 16 bit signed short integer, the largest positive number is

                          0111 1111 1111 1111

Which is (215 - 1) - because 15 bits are used for the magnitude, and it is -1 because it includes zero. In denary this is 32,767.

For a 16 bit signed short integer, the largest negative number is

                         1111 1111 1111 1111

Which is -(215) because 15 bits are used for the magnitude. In denary this is -32,768.

In a typical computer language, a signed short integer is declared like this

                         MyVariable signed short int

or just

                         MyVariable signed int

if it defaults to short integer anyway.

A long signed integer is typically declared like this

                         MyVariable signed long int

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: Range of signed integer