Back to oocities.com/technozeus

The internal mathematical language of modern digital computers.

Binary and Hexadecimal Arithmetic

You've probably heard of binary and hexadecimal, and may even know a little about what they are, but many people have asked me what they have to do with each other, or why they tend to sometimes be talked about as if they are synonymous with each other. Well, if you're one of the many people who think that it's way over your head, read on and see just how simple it really is.

First, I would like to point out that although you have probably been told taught that certain symbols always mean certain things, that is actually an exaggeration. In fact, although some symbols have generally been agreed upon to have standardized meanings under certain common circumstances, the whole idea behind symbolic representation is to be represent something of any given complexity with something less complex. Sometimes this happens in a logical or at least semi-logical manor, such as producing an acronym for a long title by stringing together the first letter of each word in that title. However, it is also possible to make such associations completely abstractly. For example, the letter "a" is used to represent different sounds in different languages, and in fact is used to represent different sounds in different words in some languages.

Another example of how the same symbol can mean different things is that the character "V" was used in English at one time as the vowel currently represented by "U" and is also the symbol used to represent the Roman numeral for the numeric value 5, which is used quite differently in the Roman Numeral system than the "5" character is used in the decimal system of numeric representation that you're probably familiar with. To be more specific about what I meant by that, consider that the Roman numeral "V" represents the same value as symbol "5" does in decimal, and the Roman numeral "I" represents the same numeric value as the symbol "1" does in decimal, and yet the number "IV" in the Roman Numeral system doesn't have the same numeric value as "15" does in decimal, but rather the numeric value of "4" because in the Roman numeral system, placing "I" before "V" rather than after it means that it is to be subtracted, and in decimal each place to the left is implied to be multiplied by ten once time for each position left of the ones place, making "15" actually represent the decimal value of "10" and the decimal value of "5" added together.

Yes, that was terribly wordy. This is why standards have been agreed on. I could have said the same thing in a lot less words if I was to assume that anyone reading it would just know what I meant by "1" and "V" and "5" and "I" but because of the topic being discussed and the changing context within that paragraph, that just wouldn't be practical to assume.

With that in mind, I'll give you a little comparison between decimal and binary, and then I'll go into binary by itself so that it will be easier to follow what I'm saying.

In decimal, moving a digit one place to the left multiplies it's implied value by the decimal value "10" which is also the binary value "1010" and I will call "ten" here for the sake of simplicity. In binary, moving a digit one place to the left multiplies it's implied value by the binary value "10" which is also the decimal value "2" and I will call "two" here for the sake of simplicity. Therefore, the decimal number "10011" and the binary number "10011" may look the same, but in fact represent very different numeric values. The decimal number "10011" represents 1 times ten times ten times ten times ten, plus 1 times ten, plus 1. The binary number "10011" represents 1 times two times two times two times two, plus 1 times two, plus 1. As you can see, these are not the same values.

In decimal, we count 1, 2, 3, 4, 5, 6, 7, 8, 9, and then having run out of one digit decimal numbers, we continue with 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, and so on. This process is familiar to most people, but fewer people really understand why.

In binary, we count 1, 10, 11, 100, 101, 110, 111, 1000, and so on. Okay, this may take a little explanation. First, I started with 1, and then having run out of one digit binary numbers I went on to 10, 11, and then having run out of two digit binary numbers, I continued on with 100, 101, 110, 111, and then having run out of three digit binary numbers we would continue with 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, and of course, then we are out of four digit binary numbers so we continue with 10000, 10001, 10010, 10011, 10100, 10101, 10110, 10111, 11000, 11001, 11010, 11011, 11100, 11101, and so on.

Well, that's it for counting. Now you may be wondering why this is so important. Okay, let me explain that in simple terms. Exactly "how much" electricity is going through an electronic circuit can be a little tricky to pin down sometimes, but it's generally much easier to answer the simple question of whether there is or is not electricity going through the circuit. This simple question, is the basis of all modern digital computers. From this simple question and it's simple set of two possible answers we can build a number system with two possible one digit values. 0 and 1. Those numeric values can be stored symbolically in an electronic circuit or for that matter in anything that can store something other than it's default state, and can be retrieved by checking the answer to a simple yes or no question about the it's current state. Modern computers check the state of many such binary storage locations at the same time, which is basically the same as reading a many digit number.

If you had only a space of up to four decimal digits to work with, you could represent ten thousand distinct numeric values. In binary, only sixteen numeric values can be represented without having to resort to adding a fifth digit. A single binary digit is called a "bit" and a group of four binary digits is called a nibble. A group of two nibbles of eight bits is generally called a "byte" and beyond that the terminology isn't really all that standardized but there are also terms for a pair of bytes and a pair of pairs of bytes and even a pair of pairs of pairs of bytes. I won't mention those terms here specifically, but I thought I should mention that they are out there, and sometimes lead to arguments due to people being unaware of the lack of standardization, but can be handy when agreed on.

Now we get to hexadecimal. In the hexadecimal system, each digit represents one nibble, so a byte is represented by a two digit hexadecimal number. This can be a lot more convenient to work with than representing the same byte with an eight digit binary number, and the nice thing about it is that they are easy to convert back and forth. For example, the binary number "1" can also be written as the binary nibble "0001" or the hexadecimal number "1" and the binary value "101" can be also be represented by the binary nibble "0101" or the hexadecimal number "5" and as you can see, there is nothing to converting a binary number with less than four digits into a binary nibble representation other than adding in leading "0" characters until there are four digits. Unlike with messy conversion between the Roman Numeral system and the decimal system, we can take this simple information and apply it to larger numbers. For example, the hexadecimal number "15" is represented by in binary as "10101" which is simply a concatenation of "0001" and "0101" into "00010101" which of course doesn't change it's value if the three leading "0" characters are removed. This is why hexadecimal and binary are sometimes used almost synonymously. They are in fact, so easily interchangeable that it's almost as it the conversion is built in.

There are only sixteen conversions to learn, and with those few conversions it is possible to convert a number of any length between binary and hexadecimal, and back. There are many conventions used to show that a number is meant to be interpreted as binary, or meant to be interpreted as hexadecimal, but again these things haven't really been standardized. For the sake of showing the sixteen conversions here, I will use only one digit hexadecimal numbers, and four digit binary numbers, so interpret the one digit numbers as hexadecimal and the four digit numbers as binary in the following list: 0000 = 0, 0001 = 1, 0010=2, 0011 = 3, 0100 = 4, 0101 = 5, 0110 = 6, 0111 = 7, 1000 = 8, 1001 = 9, 1010 = A, 1011 = B, 1100 = C, 1101 = D, 1110 = E, 1111 = F, and of course, I just ran out of four digit binary numbers, and at the same time ran out of one digit hexadecimal numbers, so if I want to continue, I would have to add a second nibble, resulting in the following eight digit binary numbers and two digit hexadecimal numbers: 00010000 = 10, 00010001 = 11, 00010010 = 12, 00010011 = 13, 00010100 = 14, 00010101 = 15, 00010110 = 16, 00010111 = 17, and so on until we reach 11111111 = FF at which point we would have run out of two digit hexadecimal numbers and eight digit binary numbers. You may have noticed that the symbols used for one digit numbers higher than 9 in hexadecimal are familiar as letters rather than as numbers, but as I mentioned earlier such symbolic meaning isn't built into the characters. It's given to them, as needed. Since symbol were needed to represent those values with a single character for the hexadecimal system, six letters of the alphabet got to double as numbers. In hexadecimal we count as follows: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, and so on.

Okay, now for addition. You should like this. While there are a hundred different pairs of one digit decimal numbers to make tables out of for such things as addition and multiplication, there are only four such pairs available in binary. Addition of single digit binary numbers therefore goes as follows: 0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 10
You may notice that if you end up in a position of having to carry one, that's not a problem even if that one has to be carried to a column where you already have to add 1 + 1, since 0 + 1 = 1 and therefore 10 + 1 = 11 because the extra one is being added to the "0" in the ones place.

Multiplication of binary numbers is also pretty simple. 0 * 0 = 0
0 * 1 = 0
1 * 0 = 0
1 * 1 = 1
Four multiplications, and that's it for single digit numbers. In short, 1 times any number is itself, and 0 times any number is zero. If you have more digits, simply multiply by each digit in turn and shift the results to the proper place value accordingly before adding them together for your final results. Same as you would do in decimal.

Division is also pretty simple, but we run into the same problem dividing by 0 as we do in decimal or in any other system.

0 / 0 is technically 1, but generally is considered undefined unless you're used to dealing with infinitesimals, where infinitely close to zero but not "exactly" zero actually has meaning.

0 / 1 = 0
1 / 0 is also undefined in general arithmetic, but is technically an infinity, and of course...
1 / 1 = 1
which you probably expected.
Of course, long division rarely involves division by zero, and the simplicity of binary multiplication makes binary division an awful lot simpler than decimal division.

Subtraction is also quite simple, but just as with decimal you can end up with negative numbers to deal with. You may expect negative 1 to be written as -1, and in fact you can write it that way, but when a computer gets a negative integer as a result, it's often represented in a much different way. I'll explain that soon, but for now, here is the list of subtractions that correspond with our addition table: 0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
10 - 1 - 1

Okay, so about those negative numbers. Since computers usually have a limited number of digits to work with, the usual solution for handling negative numbers is to negate the highest order bit. That's Geek for change the leftmost place value into the negative of it's usual value while leaving the rest be positive. This way of representing "signed numbers" makes addition and subtraction much simpler, since negative numbers can then be added and subtracted as if they were positive numbers. For example, since the signed binary byte "10000000" represents the same value as the decimal number "-128" you can add 2 decimal (or 10 binary) to get "10000010" which is the signed binary byte representation of the decimal value "-126" and you can add the binary value "11" to that byte and get "10000101" which is "-123" in decimal.

This may look complicated, but that's mostly because I'm going back and forth between two number systems. If I leave the decimal out of it, and simply count backward in eight digit signed binary numbers, starting at an arbitrary positive number, you can see clearly what happens when I pass 0 while counting down: 00011101, 00011100, 00011011, 00011010, 00011001, 00011000, 00010111, 00010110, 00010101, 00010100, 00010011, 00010010, 00010001, 00010000, 00001111, 00001110, 00001101, 00001100, 00001011, 00001010, 00001001, 00001000, 00000111, 00000110, 00000101, 00000100, 00000011, 00000010, 00000001, 00000000, 11111111, 11111110, 11111101, 11111100, 11111011, 11111010, 11111001, 11111000, and you may have noticed that -1 is represented in an eight bit signed binary number as "11111111" which is called the twos complement representation. The same value is represented in a thirty-two bit signed binary number as "11111111111111111111111111111111" which translates into decimal addition as -2147483648 + 1073741824 + 536870912 + 268435456 + 134217728 + 67108864 + 33554432 + 16777216 + 8388608 + 4194304 + 2097152 + 1048576 + 524288 + 262144 + 131072 + 65536 + 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1 and if that looks complicated you can blame it on decimal because in hexadecimal the same addition problem looks like -80000000 + 40000000 + 20000000 + 10000000 + 8000000 + 4000000 + 2000000 + 1000000 + 800000 + 400000 + 200000 + 100000 + 80000 + 40000 + 20000 + 10000 + 8000 + 4000 + 2000 + 1000 + 800 + 400 + 200 + 100 + 80 + 40 + 20 + 10 + 8 + 4 + 2 + 1 = -1 which is still thirty-two additions but as you can probably see, much less complicated than the same addition in decimal.

One more piece of information that may be helpful to know. The word hexadecimal is actually formed from word parts meaning six and ten, because in hexadecimal there are sixteen possible values for a single digit, which is six more than in decimal. You may sometimes see people shortening the word down to just "hex" which is much less accurately descriptive, but easier to say and write. Since base sixteen is much more commonly used than base six, this is generally not a problem, but once again this is simply a case of a simpler symbolic form being used as if it were standard in the absence of any objections to such usage.

Donald A. Kronos, PhD.

Back to oocities.com/technozeus