HOME

Hexadecimal Numbers

(Jump to Bitmask Code info)

Most of this info has been shamelessly ripped from Discovering BBC Micro Machine Code by A.P.Stephenson.
The book is years out of print, and the machine it refers to is similarly lost in the mists of time.���(I am, however, the proud owner of two of them.)���I wrote a letter to the publisher, Granada Publishing Ltd., to ask permission, but the letter came back stamped with the information that they had "gone away".���So I don't suppose anyone will mind :D.

(Chapter Two - Number Representation.)

I have left out all the bits about negative numbers, and all points specific to the BBC Micro.���What is left is the best guide to hexadecimal and binary numbers I have ever seen.���If anyone doesn't understand it, I can't help.

Text in this font is my own work.
Text in this font is ripped.


Hexadecimal Numbers

There are two types of computer - analogue and digital, the digital being the more common.���Analogue computers usually work by storing numbers as electrical voltages.���They measure the voltages.���Analogue computers are measuring machines, and although they are very fast, they are far less accurate than digital computers like the Game Boy.

The digital computer is essentially a counting machine.���It is always possible to count accurately.���You can say with absolute certainty whether a voltage is there or is not there.���You can tell whether a switch is ON or OFF.���Digital computers rely on detecting whether a "switch" is ON or OFF.���In fact, a digital computer is nothing more than a gigantic bank of switches - not the type of switch you can handle physically, of course, but tiny electrical circuits which are two-state, either in one state or the other.���By counting the number of circuits in the ON state, it is possible for the computer to treat the result numerically.


Binary

Binary is a two-state counting system using only two characters 1 and 0 and is therefore the system of choice.���It may be tedious for humans, used to ten characters 0,1,2...9 but tedium is unimportant to those tiny circuits.���When first confronted with binary it is a little startling to see strange arithmetic statements like 1 + 1 = 10 and even worse to learn that 10 + 10 = 100.���However, this will make sense when we memorise the simple progression, 1,2,4,8,16,32,64... etc, and understand that the "weighting" of any binary digit increases by a power of two from right to left, instead of a power of ten as in the familiar decimal system.

Examples :

11 in binary is a 1 and a 2 in decimal, representing 3.
101 in binary is a 1 and a 4 in decimal, representing 5.
1111 in binary is a 1 and a 2 and a 4 and an 8, representing 15.

A rather important binary number is 11111111 which you should now be able to translate to 255 in decimal.���This number continuously appears in computing manuals and textbooks.

Addition in binary is exactly the same as in decimal except that a carry to the next column takes place when the sum exceeds 1 rather than 9.

Examples are better than wordy descriptions, so spend a few minutes in order to agree that the following makes sense :

3�=�0011
+ 2�=�0010
5�=�0101
�7�=�0111
+ �8�=�1000
15�=�1111
�5�=�0101
+ �6�=�0110
11�=�1011


The Byte

The meaning of the term byte in microcomputing is a string of eight bits.���A "bit" (a corruption of BInary digiT) is a 1 or a 0.

The byte is very important because it is the conventional size of memory unit that the memory of computers is seen to be broken up into.���Each memory location in a computer has its own address, and each is one byte.

Each tiny cell in a byte can store a 1 or a 0 so, if all of them contain a 1, you have the binary number 11111111, which as we have already seen is 255 in decimal.���There is another way of stating binary numbers called two's complement which covers positive and negative numbers, (and then 11111111 is not 255) but I have never seen it used on the Game Boy so I won't go into it here.

For convenience and clarity, it is customary to consider the byte as if it were made up of two equal 4-bit halves called nibbles.���The separation has no real significance to the computing circuits.


Hexadecimal Notation (hex)

It was necessary to find some quick method of describing the contents of a byte.���We could, of course, describe it in terms of the decimal equivalent but this is not quick enough.���Perhaps you can, but I certainly couldn't, blurt out instantly the value of 1011 1101.���I could, however, describe it as BD hex almost instantly and probably you will be able to as well - soon.

Hexadecimal is Latin (or maybe Greek) for "sixteen."���It is a counting system, like binary and decimal, but uses sixteen characters :

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

A nibble is a block of four bits, so there must be 24 (sixteen) different combinations of arranging the bits in a nibble.���Each combination can be uniquely represented by one hex digit as follows :

Binary Hex Binary Hex
0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 3 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F


Examples :

1111 1111 = FF
1010 0001 = A1
1000 0000 = 80
1011 0011 = B3

Although used primarily for describing the contents of a byte in terms of a pair of hex digits, we must not forget that it is a legitimate counting system (although a little cumbersome).���When adding two hex numbers, a carry is required to the next higher place when the sum exceeds 15 in decimal (F in hex).���Care must be taken in interpreting hex in relation to decimal so the following examples may help :

10 in hex is 16 in decimal.
32 in hex is 50 in decimal.
6F in hex is 111 in decimal.
FFFF in hex is 65 535 in decimal.

To understand these conversions examine the following place weightings of four-hex-digit numbers which progress to the left in increasing powers of 16 :

163 162 161 160 (4096, 256, 16, 1)
Example : �0 �0 �5 �F (5x16) + (15x1) = 95 decimal
Example : �0 �2 �D �A (2x256) + (13x16) + (10x1) = 730 decimal
Example : �F �F �F �F (15x4096) + (15x256) + (15x16) + (15x1) = 65 535


Here are a few examples in the addition of two hex numbers :

�F �FF AF ABCD
�3 �01 3C 0111
12 100 EB ACDE


Manipulations like the above may take some while before they can be carried out instinctively although, fortunately, the majority of hex arithmetic is carried out on one-byte numbers which require only two hex digits.


Bit Positions Within a Byte

The bit at the extreme right of the diagram is referred to as the lsb (Least Significant Bit).���The bit on the extreme left is the msb (Most Significant Bit).���Note that the bits are numbered bit-0 to bit-7 for reference purposes so the lsb is bit-0 and the msb is bit-7.


7 6 5 4 3 2 1 0
^ ^
msb lsb


Maximum Capacity of One Byte

It is clear from the above that a single byte is limited in the size of numbers it can handle.���The largest number possible is FF hex (255 decimal).
If larger numbers have to be handled it is necessary to reserve more than one byte per number.

Example :
0111 1111��1111 1111 would be 32 767 in decimal (7FFF hex).


Summary



Bitmask codes

Some variables in a game hold data as a 1 if something is there and a 0 if it is not.���This method is easier to perform calculations with, but is extremely wasteful of memory.
However, there is another way.���One byte can be used to store up to eight 0-or-1 values, for the simple reason that a byte is made up of eight 0-or-1 bits.���The problem is that the data does not look like this on an Action Replay or GameShark.���It is shown as a normal-looking hexadecimal number.

I will use the Badges for Red / Blue as an example.
The address to write to is 56D3, so the code is 01 xx 56D3.
I have written that the Earth Badge is the msb (most significant bit).���Put that on the left.
The Boulder Badge is the lsb (least significant bit).���Put that on the right.
Supposing you want the Rainbow Badge (4th), Soul Badge (6th), and Earth Badge (8th).
List the badges in order from msb to lsb.
Write a 1 under the ones you want, and a 0 under the ones you don't.
For example,
8 7 6 5��4 3 2 1
1 0 1 0���1 0 0 0
You now have a binary number.
Convert it into hexadecimal.
In this case it is A8.
You now have the data to substitute in for xx.
Note that all the badges would give binary number 1111 1111, or FF hex.


HOME
Hosted by www.Geocities.ws

1