Hexadecimal

From Custom Mario Kart
Jump to: navigation, search

The Hexadecimal system is only another printable representation of numbers (most integers) with 16 digits. Human people prefer the decimal system with 10 digits because they have 10 fingers. Furthermore hexadecimal numbers are used to present any data on computers.



Introduction

The following 3 paragraphs describe very short decimal, binary and hexadecimal numbers. And the only difference is the base (=number of digits) of the representation: 10 for decimal, 2 for binary and 16 for hexadecimal.

→ Read the Wikipedia page »Hexadecimal« for more details.

Decimal

The humans use the decimal system since many centuries to represent numbers. The system is very easy, each child learn it in an early school.

  • Decimal numbers use 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
  • Decimal numbers are combinations of the 10 digits like 10 or 9876. (We use here only integral numbers without fractions).
    • The number 10 means: 1*10 + 0
    • The number 9876 means: 9*1000 + 8*100 + 7*10 + 6

Binary

Computers are based on bits. A bit has 2 states: off and on (or minus and plus). The normal representation is to use the 2 digits 0 and 1.

  • Binary numbers use only 2 digits: 0 and 1.
  • Binary numbers are combinations of the 2 digits like 0b10 , 0b1001 or 0b1111.
    • This article uses the prefix '0b' to distinguish binary number from other based numbers.
    • The number 0b10 means: 1*2 + 0
    • The number 0b1001 means: 1*8 + 0*4 + 0*2 + 1 == 11
    • The number 0b1111 means: 1*8 + 1*4 + 1*2 + 1 == 15

Hexadecimal

Because binary numbers tend to be long strings, hexadecimal numbers are used to represent binary values.

  • Hexadecimal numbers use 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
    • Because of the lack of real digits, the first alphabetic letters are used. Mostly lower case letters are allowed too.
    • Exact 4 binary digits are combined to 1 hexadecimal digit. 0b0000 becomes 0x0 and 0b1111 becomes 0xf.
  • Hexadecimal numbers are combinations of the 16 digits like 0x10, 0xA3 or 0x9876.
    • The prefix '0x' comes from Unix and C to distinguish decimal and hexadecimal numbers. This article follows this rule.
    • The number 0x10 means: 1*0x10 + 0 == 1*16 + 0 == 16
    • The number 0xA3 means: 0xA*0x10 + 3 == 10*16 + 3 == 163
    • The number 0x9876 means: 9*0x1000 + 8*0x100 + 7*0x10 + 6 == 9*4096 + 8*256 + 7*16 + 6 == 39030

Advantages of hexadecimal numbers

Computers use octets to represent data. An octet is a collection of 8 bits. The todays popular naming for an octet is byte, because most modern computers use 8 bits per byte.

The advantage of a hexadecimal number is, that an octet can be represented as exact 2 hexadecimal digits. This is important, if you want to write down the value of 2 bytes. In the following example, the binary and hexadecimal representation vary only in spacing. But in decimal you get a total different number:

               2 single bytes   : 16 bits as 1 word
----------------------------------------------------
 Binary:      00010010 00110100 : 0001001000110100
 Hexadecimal:       12 34       :       1234
 Decimal:           18 52       :       4660

A second advantage of hexadecimal numbers is, that an experienced user can directly see, which bits are set and which not. This is important when single bits have special meanings or for bitwise operations.

Hexadecimal numbers are often used to demonstrate bitwise operations.

Glossary

AND
Bitwise operation with 2 operands: A resulting bit is 1, if both operands are also 1. C operator is '&'.
Binary 
A representation of numbers with 2 digits.
Bit 
A bit is the smallest information unit on computers. It is either 0 (off) or 1 (on).
Byte 
A byte is the smallest machine word. Nowadays it is always 1 octet (8 bits), but there are also some old systems where a byte have 4 or 9 bits.
Decimal 
A representation of numbers with 10 digits.
Dual 
An alternative name for Binary: A representation of numbers with 2 digits.
EOR
Bitwise operation with 2 operands (same as XOR): A resulting bit is 1, if exact one of the operands is is 1 (exclusive OR). C operator is '^'.
Hex
A short cut for Hexadecimal: A representation of numbers with 16 digits.
Hexadecimal 
A representation of numbers with 16 digits.
int
  • In C#: An integer number with 4 bytes.
  • In C/C++: An integer number with 2 or 4 bytes. The real size is machine dependent. The size of int is always greater or equal than the size of short.
long
  • In C#: An integer number with 8 bytes.
  • In C/C++: An integer number with 4 or more bytes. The real size is machine dependent. The size of long is always greater or equal than the size of int.
long long 
A C/C++ data type: An integer number with 4 or more bytes. The real size is machine dependent. The size of long long is always greater or equal than the size of long.
LONG WORD 
An inexact name for 4 bytes (Microsoft).
Nibble 
A nibble is a collection of 4 bits. You can see it as solute for a hexadecimal digit.
NOT
Bitwise inversion of all bits. C operator is '~'.
OCT WORD 
An inexact name for 8 bytes (Microsoft).
Octal 
A representation of numbers with 8 digits.
Octet 
An octet is a collection of 8 bits.
OR
Bitwise operation with 2 operands: A resulting bit is 1, if at least one of the operands is 1 (inclusive OR). C operator is '|'.
s16
A C/C++ user defined data type: This signed 16 bit value is used very often in programs to access external binary data.
s32
A C/C++ user defined data type: This signed 32 bit value is used very often in programs to access external binary data.
s8
A C/C++ user defined data type: This signed 8 bit value is used very often in programs to access external binary data.
short
  • In C#: An integer number with 2 bytes.
  • In C/C++: An integer number with 1 or 2 bytes. The real size is machine dependent.
u16
A C/C++ user defined data type: This unsigned 16 bit value is used very often in programs to access external binary data.
u32
A C/C++ user defined data type: This unsigned 32 bit value is used very often in programs to access external binary data.
u8
A C/C++ user defined data type: This unsigned 8 bit value is used very often in programs to access external binary data.
WORD 
An inexact name for 2 bytes (Microsoft).
XOR
Bitwise operation with 2 operands (same as EOR): A resulting bit is 1, if exact one of the operands is is 1 (exclusive OR). C operator is '^'.