Section 04 Part 02 – Signed and Unsigned

 

A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” ~Herm Albright, quoted in Reader's Digest, June 1995

 

 

 

Introduction

 

In mathematics, you can have negative numbers (-1, -2, -3, etc).  This section looks at signed and unsigned, and how they can be used in programming to “fake” (or rather pretend) that negative numbers exist.

 

 

 

Unsigned

 

Right now, you’ll most probably agree with me, that for a byte of data; 00 is the lowest number, and FF Is the highest number.  For a word of data; 0000 is the lowest number, and FFFF is the highest number.  And for a long-word of data; 00000000 is the lowest number, and FFFFFFFF is the highest number.

 

I hope we’re cool with that so far...

 

Now, this is what we refer to as “unsigned”.  Unsigned numbers are always positive from 00, right up to FF.

 

 

 

Signed

 

There will be times however, where you’ll need negative numbers to get some sort of calculation done.  This is where “signed” comes in.

 

When a byte of data is treated as signed; 00 to 7F is treated as positive, while 80 to FF is treated as negative:

 

Unsigned

Lowest number

Highest number

00 01 02 03 04 05 06 07 ...

... F8 F9 FA FB FC FD FE FF

 

Signed

Lowest number

Highest number

80 81 82 83 ...

... FC FD FE FF 00 01 02 03 04 ...

... 7C 7D 7E 7F

 

As you can see for “signed” the numbers 80 to FF are red for negative:

 

...

03

=

+3

02

=

+2

01

=

+1

00

=

0

FF

=

-1

FE

=

-2

FD

=

-3

...

 

This is a process known as "Two's Complement", it is the idea of using the MSB (most significant bit) to signify the value is negative:

 

80 = 1000 0000

 

All numbers from 80 to FF will have that bit 1 (set), while the positive numbers from 00 to 7F will have it 0 (clear).

 

For word and long-word, the same applies:

 

8000 = 1000 0000 0000 0000

 

 

80000000 = 1000 0000 0000 0000 0000 0000 0000 0000

 

It’s important not to confuse byte, word and long-word at this stage, for good reasons.

 

The byte FC is -4, but if you were to read FC as a word; 00FC, then the number is no longer negative, it is read as positive for word because it is between 0000 and 7FFF.  To have it as -4 for word, you need to extend it as FFFC.  The same goes for long-word, the number needs to be extended.  FFFFFFFC is -4 for long-word.

 

 

 

Conversion

 

It should be made very clear however, that simply changing the MSB will not convert the polarity of a number from positive to negative, or negative to positive directly.  For example, the number 04 in binary 0000 0100.  Setting the MSB will give us 1000 0100 in hex 84, while 84 is a negative number, it is not -4, it is in fact -7C.

 

Negative numbers start from FF (which is -1), and proceed downwards, FE (-2), FD (-3), FC (-4), and so on...

 

There is of course a simpler method to convert a number from positive to negative (or negative to positive), which can be done by performing a "logical compliment" onto the number (you may remember the "NOT" instruction), and then adding 1 to the number afterwards, for example, the positive number 04 (0000 0100), perform "logical compliment" by swapping the bits:

 

          0000 0100

 

          |||| ||||

          VVVV VVVV

 

          1111 1011

 

This gives us FB (1111 1011), then adding 1 to it will give us FC (-4), and that is the correct conversion, it also works for converting negative numbers into positive.  It is a strong belief that the CPU does this internally for the "NEG" instruction (which will be explained shortly in the next part).

 

 

 

Further info

 

Your next question may probably be “how do I treat a number as unsigned or signed?”.  It’s a good question, the answer is simple, but not easy to explain, so bear with me please.

 

The 68k has what is known as a “Conditional Code Register” (CCR), and every time an instruction is processed, information is saved to the CCR, this information can include:

 

 

As you can see, information for both signed and unsigned are stored away in the CCR.  So to sum it up, the number is treated as both signed and unsigned in most cases.

 

There are instructions that we’ll get to later on, that will read the CCR and perform an action depending on the signed or unsigned results.  So you’ll have complete control over how the number is treated.

 

Important: The above information about the CCR is very minor; more information on the CCR will be explained later on in time.  It is explained here briefly so you can understand when a number is treated as signed or unsigned, and nothing more.  So don’t worry if you don’t quite understand the CCR just now, you’re not expected to.

 

 

 

Previous Part

Main Page

Next Part