 Erik Østergaard - Hexadecimal Number System    ### The Hexadecimal Number Base System

A big problem with the binary system is verbosity. To represent the value 202 requires eight binary digits.

The decimal version requires only three decimal digits and, thus, represents numbers much more compactly than does the binary numbering system. This fact was not lost on the engineers who designed binary computer systems.

When dealing with large values, binary numbers quickly become too unwieldy. The hexadecimal (base 16) numbering system solves these problems. Hexadecimal numbers offer the two features:

• hex numbers are very compact
• it is easy to convert from hex to binary and binary to hex.

Since we'll often need to enter hexadecimal numbers into the computer system, we'll need a different mechanism for representing hexadecimal numbers since you cannot enter a subscript to denote the radix of the associated value.

The Hexadecimal system is based on the binary system using a Nibble or 4-bit boundary. In Assembly Language programming, most assemblers require the first digit of a hexadecimal number to be 0, and we place an H at the end of the number to denote the number base.

uses base 16
includes only the digits 0 through 9 and the letters A, B, C, D, E, and F

In the Hexadecimal number system, the hex values greater than 9 carry the following decimal value:

 Binary Octal Decimal Hex 0000B 00Q 00 00H 0001B 01Q 01 01H 0010B 02Q 02 02H 0011B 03Q 03 03H 0100B 04Q 04 04H 0101B 05Q 05 05H 0110B 06Q 06 06H 0111B 07Q 07 07H 1000B 10Q 08 08H 1001B 11Q 09 09H 1010B 12Q 10 0AH 1011B 13Q 11 0BH 1100B 14Q 12 0CH 1101B 15Q 13 0DH 1110B 16Q 14 0EH 1111B 17Q 15 0FH 1 0000B 20Q 16 10H

This table provides all the information you'll ever need to convert from one number base into any other number base for the decimal values from 0 to 16.

To convert a hexadecimal number into a binary number, simply brake the binary number into 4-bit groups beginning with the LSB and substitute the corresponding four bits in binary for each hexadecimal digit in the number.

For example, to convert 0ABCDh into a binary value, simply convert each hexadecimal digit according to the table above. The binary equivalent is:

 0ABCDH = 0000 1010 1011 1100 1101

To convert a binary number into hexadecimal format is almost as easy. The first step is to pad the binary number with leading zeros to make sure that the the binary number contains multiples of four bits. For example, given the binary number 10 1100 1010, the first step would be to add two bits in the MSB position so that it contains 12 bits. The revised binary value is 0010 1100 1010.

The next step is to separate the binary value into groups of four bits, e.g., 0010 1100 1010. Finally, look up these binary values in the table above and substitute the appropriate hexadecimal digits, e.g., 2CA.

The weighted values for each position is as follows:

 16^3 16^2 16^1 16^0 4096 256 16 1

### Binary to Hex Conversion

It is easy to convert from an integer binary number to hex. This is accomplished by:

1. Break the binary number into 4-bit sections from the LSB to the MSB.
2. Convert the 4-bit binary number to its Hex equivalent.

For example, the binary value 1010111110110010 will be written:

 1010 1111 1011 0010 A F B 2

### Hex to Binary Conversion

It is also easy to convert from an integer hex number to binary. This is accomplished by:

1. Convert the Hex number to its 4-bit binary equivalent.
2. Combine the 4-bit sections by removing the spaces.

For example, the hex value 0AFB2 will be written:

 A F B 2 1010 1111 1011 0010

This yields the binary number 1010111110110010 or 1010 1111 1011 0010 in our more readable format.

### Hex to Decimal Conversion

To convert from Hex to Decimal, multiply the value in each position by its hex weight and add each value. Using the value from the previous example, 0AFB2H, we would expect to obtain the decimal value 44978.

 A*16^3 F*16^2 B*16^1 2*16^0 10*4096 15*256 11*16 2*1 40960 3840 176 2

40960 + 3840 + 176 + 2 = 44978

### Decimal to Hex Conversion

To convert decimal to hex is slightly more difficult. The typical method to convert from decimal to hex is repeated division by 16. While we may also use repeated subtraction by the weighted position value, it is more difficult for large decimal numbers.

### Repeated Division By 16

For this method, divide the decimal number by 16, and write the remainder on the side as the least significant digit. This process is continued by dividing the quotient by 16 and writing the remainder until the quotient is 0. When performing the division, the remainders which will represent the hex equivalent of the decimal number are written beginning at the least significant digit (right) and each new digit is written to the next more significant digit (the left) of the previous digit. Consider the number 44978.

 Division Quotient Remainder Hex Number 44978 / 16 2811 2 2 2811 / 16 175 11 B2 175 / 16 10 15 FB2 10 / 16 0 10 0AFB2

As you can see, we are back with the original number. That is what we should expect.

When you use hex numbers in an 8085 program, the Assembler usually requires the most significant hex digit to be 0 even if this number of digits exceed the size of the register. This is an Assembler requirement and your value will be assembled correctly.

#### Working with Logarithms / Udregning ved hjælp af logaritmer

A logarithm is used when working with exponentiation. We all learned that the formula `X = YZ` means take the value `Y` and multiply it by itself the number of times specified by `Z`. For example, `23 = 8` (`2*2*2`). The value `Z` is the exponential value of the equation. As long as you know what the `Y` and `Z` values are in the equation, it is easy to calculate the value of `X`. / En logaritme bruges, når der udregnes med eksponent. Vi lærte alle, at formlen `X = YZ` betyder, tag værdien `Y` og multiplicer (gang) den med sig selv antallet af gange angivet af `Z`. For eksempel, `23 = 8` (`2*2*2`). Værdien `Z` er eksponentiel-værdien i ligningen. Så længe man kender, hvad `Y` og `Z` værdierne er i ligningen, er det nemt at beregne værdien af `X`.

Unfortunately, you may not always know the value of `Y` and `Z`. How do you determine `Z` if you know the value of `X` and `Y`? This is when you use a logarithm. A logarithm is the exponent value that indicates the number of times the value `Y` needs to be multiplied by itself to get the value `X`. The value that is multiplied (`Y`) is considered to be the base of the formula. / Uheldigvis kender man ikke altid værdien af `Y` og `Z`. Hvordan bestemmer man `Z`, hvis man kender værdien af `X` og `Y`? Det er da, man bruger en logaritme. En logaritme er eksponent-værdien, som angiver antallet af gange værdien `Y` behøver at blive multipliceret (ganget) med sig selv for at få værdien `X`. Værdien som er multipliceret (ganget) (`Y`) betragtes som grundtallet i formlen.

There are two basic types of logarithms: common and natural. A common logarithm uses a value 10 as the base value. Therefore, in the basic formula for exponentiation above, `X = YZ`, the value of `Y` is 10, and `Z` is the number of times that `Y` needs to be multiplied by itself to return the value indicated by `X`. / Der er to grundlæggende typer af logaritmer: sædvanlig og naturlig. En sædvanlig logaritme bruger en værdi 10 som grundtal. Derfor i den grundlæggende eksponent-formel ovenfor, `X = YZ`, er værdien af `Y` 10, og `Z` er antallet af gange som `Y` behøver at blive multipliceret (ganget) med sig selv for at returnere værdien angivet af `X`.

Natural logarithms use a base value of approximately 2.71828182845905, normally referred to as `e`. The mathematical notation `e` is Euler's constant, the base of natural algorithms, made common by the mathematician Leonhard Euler ( Basel, Switzerland April 15, 1707 - Russia September 18, 1783). VBScript provides two functions for working with logarithms: `Exp()` and `Log()`. Each of these functions assumes that the base value is `e`. The `Log()` function returns the natural logarithm of the supplied numeric expression, and the `Exp()` function raises the supplied numeric expression to `e`. The similar methods in JavaScript is called: `Math.exp()` and `Math.log()`. / Naturlige logaritmer bruger et grundtal på tilnærmelsesvis 2,71828182845905, i reglen henvist til som `e`. Den matematiske notation `e` er Eulers konstant, de naturlige algoritmers grundtal, gjort alminding af matematikeren Leonhard Euler ( Basel, Schweiz 15. april 1707 - Rusland 18. september 1783). VBScript har to funktioner til udregninger med logaritmer: `Exp()` og `Log()`. Hver af disse funktioner antager, at grundtallet er `e`. `Log()` funktionen returnerer den naturlige logaritme til det leverede numeriske udtryk, og `Exp()` funktionen opløfter det leverede numeriske udtryk til `e`. De lignende metoder i JavaScript kaldes: `Math.exp()` og `Math.log()`.

It is possible to use these VBScript functions or JavaScript methods if you have a different base value by using a simple formula. By dividing the natural log of the desired number (`X`) by the natural log of the desired base (`Y`), you can determine the desired logarithm value (`Z`) in VBScript: `Z = Log(X) / Log(Y)` or similar in JavaScript: `Z = ((Math.log(X)) / (Math.log(Y)));`. / Det er muligt at bruge disse VBScript funktioner eller JavaScript metoder, hvis man har et andet grundtal ved at bruge en simpel formel. Ved at dividere den naturlige log til det ønskede tal (`X`) med den naturlige log til det ønskede grundtal (`Y`), kan man bestemme den ønskede logaritme-værdi (`Z`) i VBScript: `Z = Log(X) / Log(Y)` eller lignende i JavaScript: `Z = ((Math.log(X)) / (Math.log(Y)));`.

##### JavaScript comments: / JavaScript bemærkninger:

The custom function `Pow2(NumDbl)`, which returns the base to an exponent power of 2, and the custom function `Log2(NumDbl)`, which calculates base-2 logarithms, can be seen in this page's source code. They uses respectively the JavaScript `Math.pow()` method, which returns base to the exponent power, that is, base exponent, and a formula based on the JavaScript `Math.log()` method, which returns the natural logarithm (base E) of a number. / Funktionen lavet på bestilling `Pow2(NumDbl)`, som returnerer grundtallet til en eksponent potens af 2, og funktionen lavet på bestilling `Log2(NumDbl)`, som beregner grundtal-2 logaritmer, kan ses i denne sides kildekode. De bruger henholdsvis JavaScript `Math.pow()` metoden, som returnerer grundtallet til en eksponent potens, det vil sige grundtal eksponent, og en formel baseret på JavaScript `Math.log()` metoden, som returnerer den naturlige logaritme (grundtal E) af et tal.

##### See the JavaScript by View Source / Se JavaScript'et via Vis Kilde

You can see the JavaScript by using View Source. / Man kan se JavaScript'et ved at bruge Vis Kilde.

##### My Sources / Mine kilder

Sources: Various books, the Internet, and various encyclopedias.

Kilder: Forskellige bøger, internettet og forskellige leksikoner.

### Computer Data Representation and Number Systems / Computer data repræsentation og talsystemer      ©1997 - 1999 Erik Østergaard, Copenhagen, Denmark.