Bits Bytes and Number Systems- Binary, Denary, Hexadecimal, Octal and ASCII Codes tutorial

Programming Tutorial

Updated 22 Feb 2004
Tutorials: HTML and Web Design | C programming | Smalltalk Programming

Contents

Introduction

This tutorial is going to assist you with the different numbering systems that are used in programming. Programmers will need to be familiar with most of them.

You are already familiar with our every day denary numbering system based on the unit 10. Computers only understand the binary number system based on the unit 2 denary. Programming often uses the hexadecimal system base on a unit of 16 denary. For example HTML uses hexadecimal numbering in the color attribute, e.g. color="FF00FF". Another less common system is octal based on 8 denary.

You should know how to convert between the different systems. The ASCII table at the foot of this page is useful for a quick conversions up to 127 denary. The windows calculator (in scientific mode) can be used for conversions. College and University students may find that calculators are not allowed in the exam, OU course T223 is an example of this, therefore manual conversion calculations must be made. This should be practiced to speed up the process, the calculator only used to check your manual conversion.

Bit

A binary digit is called a bit. Usually expressed as 0 and 1 the two numbers of the binary numbering system.
A bit is the smallest unit of information a computer can use. A 16 bit computer would process a series of 16 bits,such as
0100111101011000 in one go, repeating the process thousands or millions of times per second.

Reading a series of bits is very difficult and to make this process easier they are often displayed in groups of 4 bits
0100 1111 0101 1000

This grouping is quite interesting in that a group of 4 bits can be replaced by a single hexadecimal digit
Two groups of 4 bits, i.e. 8 bits ( a byte) can be replaced by 2 hexadecimal digits,
and 4 hexadecimal digits are required to replace all 16 bits.

Binary
0100
1111
0101
1000
Hexadecimal
4
F
5
8

 

 

Byte

A group of 8 bits are in a byte. With 8 bits ( binary digits ), there exists 256 possible denary combinations.
If you remember that 1 byte can store one alphabetical letter, single digit, or a single character/symbol, such as #.
Large numbers of bytes can be expressed by kilobytes, megabytes etc

1 byte of memory can normally hold one of the following:

  • a single alphabetical letter (upper or lower case),
  • a single number 0-9
  • a symbol ( _ + £ # > etc
  • a further 127 alternative characters. These could be the letters used in foreign languages, lines to produce boxes etc.

See the ASCII codes at the foot of this page which shows how the first 127 characters of 256 characters are used

Kilobyte

The value of a kilobyte is 1024. Worked out as 2^10. Normally Kilo refers to 1000 but in computing kilobyte is 1024.

Top

Megabyte (MB)

Likewise, 1024Kb is referred to as a "Megabyte". Normally a Mega refers to a million. In computing 1 Mega byte is 1,048,576 bytes. Worked out as 220, or 1024*1024.

A megabyte can store roughly 4 books of 200 pages

Gigabyte (GB)

A Gigabyte is 1,073,741,824 (230) bytes. 1,024 Megabytes, or 1,048,576 Kilobytes.

A gigabyte can store almost 4500 books of 200 pages

You could store 1 but not 2 650MB CDs

Terabyte (TB)

A Terabyte is 1,099,511,627,776 (240) bytes, 1,024 Gigabytes, or 1,048,576 Megabytes.

 

Petabyte (PB)

250 bytes

Exabyte (EB)

260 bytes bytes

 

Number Systems

The every day number system we use is the denary system, sometimes called the decimal system. In programming three number systems are commonly used, binary, hexadecimal and to a lesser extent octal .

In every day life we Usually use the denary number system which has a base of 10.
But we also use other number systems, think of time (base 60, and base 10 within it), imperial distance yards and feet (base 3), there are many others, dates probably being the hardest number system to do calculations with.


In the denary binary octal hexadecimal systems, the value of any digit in a number depends on its position within that number. I.e. which column it is in.

How do numbering systems work

 To understand this we will examine the Denary system in more detail. Because you are so used to the denary system and because it is very easy to multiply by 10, 100, or a 1000 etc you calculate the number in your head.  Lets use the number 256 as an example. The calculation that is automatically done is the following

The most important calculation to do is to work out the positional values for that system. The positional value is based on the powers of the number systems base value

Power of the base
104
103
102
101
100
Calculation
10x10x10x10
10x10x10
10x10
The value of the base number
Any number to the power 0 is always 1
= Position value
10,000
1000
100
10
1

 

Write down the Positional values for the number system you are using so for Denary we would write

Position value
10,000
1000
100
10
1

Underneath the correct positional value write in your number

Position value
10,000
1000
100
10
1
Enter Number
2
5
6

The calculation that is done

Position value
10,000
1000
100
10
1
Enter Number
2
5
6
Required Calculation    
100x2
10x5
1x6
This equals    
200
50
6

Add the 3 results 200 + 50 + 6

200

50

6
____
256

You can convert a number in any number system to a denary number using this calculation. Ensure you use the positional values for the number system you are using

 

Denary Numbering (base 10) sometimes called Decimal system

The decimal system name should not be used because of confusion that this could be thought as introducing the decimal point and money systems

Uses numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, that's 10 numbers. Hence base 10. Radix is another name for base 10.
Adding 1 to 9 we must introduce an additional column to the left i.e. 10

Power of the base
104
103
102
101
100
Position value
10,000
1000
100
10
1

A byte of memory can store a number in the range 0 to 255 Denary.

Top

Binary Numbering (base 2)

A series of eight bits strung together makes a byte, much as 12 makes a dozen. With 8 bits, or 8 binary digits, there exist 2^8=256 possible combinations
Uses numbers 0, 1,, that's 2 numbers. Hence base 2.

Binary numbering is the number system that is used by computers.

Note: The positional value doubles as you go to the next positional on the left

Power of the base
24
23
22
21
20
Positional value
16
8
4
2
1

A binary digit is called a bit. There are two possible states in a bit, Usually expressed as 0 and 1, the two numbers used in the binary number system.

But the bit could represent on / off of an electrical circuit, yes / no, true / false, -1 / 0, -1 / +1, zero / non zero, or similar 2 state binary wording.

A byte of memory can store a number in the range 00000000 to 11111111 binary.
Numbers are often displayed in groups of 4, as follows, to make them easier to read. 0000 0000 to 1111 1111 binary.

How to Convert Binary to Denary

Convert  1011 binary to denary

Positional value
16
8
4
2
1
Enter Number  
1
0
1
1
Required Calculation  
8x1
4x0
2x1
1x1
This equals  
8
0
2
1

Add the 3 results 8 + 0 + 2 + 1

8

0

2
1
____
11

Therefore 1011(One Zero One One) binary = 11 (Eleven) Denary

Top

Hexadecimal Numbering (base 16)

Uses numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, that's 16 numbers. Hence base 16..
A=10 Dec, B=11 DEC, C=12 DEC, D=13 DEC, E=14 DEC, F=15 Dec.
By using the letters A-F a single digit only requires a single position (column).
Power of the base
164
163
162
161
160
Positional value
65536
4,096
256
16
1

A byte of memory can store a number in the range 00 to FF Hex

 

Hexadecimal numbers are often displayed in groups of 2, to make them easier to read. E.g.
10 AF 3C 9F

A single hexadecimal number requires 4 units of binary numbers. This makes it reasonably easy to convert between these two numbering systems. E.g.

1 = 0001

9 = 0101

A = 1010

 

Top

Octal (base 8)


Uses numbers 0, 1, 2, 3, 4, 5, 6, 7, , that's 8 numbers. Hence base 8..
A byte of memory can store a number in the range 0 to 377 Oct.
Power of the base
84
83
82
81
80
Positional value
4096
512
64
8
1

Binary Systems

The Binary system

  • is used by digital devices such as computers.
  • Consists of two possible states such as On - Off, Yes - No, True - False, Zero - Non Zero and possibly more confusing in programming -1 and 0.

These states are usually represented by either the number 0 or 1 of the binary number system.

Top

ASCII Codes

ASCII DEC Hex Octal Binary
NULL 000 00 000 0000 0000
SOH 001 01 001 0000 0001
STX 002 02 002 0000 0010
ETX 003 03 003 0000 0011
EOT 004 04 004 0000 0100
ENQ 005 05 005 0000 0101
ACK 006 06 006 0000 0110
BEL 007 07 007 0000 0111
BS 008 08 010 0000 1000
HT 009 09 011 0000 1001
LF 010 0A 012 0000 1010
VT 011 0B 013 0000 1011
FF 012 0C 014 0000 1100
CR 013 0D 015 0000 1101
SO 014 0E 016 0000 1110
SI 015 0F 017 0000 1111
DLE 016 10 020 0001 0000
DC1 017 11 021 0001 0001
DC2 018 12 022 0001 0010
DC3 019 13 023 0001 0011
DC4 020 14 024 0001 0100
NAK 021 15 025 0001 0101
SYN 022 16 026 0001 0110
ETB 023 17 027 0001 0111
CAN 024 18 030 0001 1000
EM 025 19 031 0001 1001
SUB 026 1A 032 0001 1010
ESC 027 1B 033 0001 1011
FS 028 1C 034 0001 1100
GS 029 1D 035 0001 1101
RS 030 1E 036 0001 1110
US 031 1F 037 0001 1111
space 032 20 040 0010 0000
! 033 21 041 0010 0001
" 034 22 042 0010 0010
# 035 23 043 0010 0011
$ 036 24 044 0010 0100
% 037 25 045 0010 0101
& 038 26 046 0010 0110
' 039 27 047 0010 0111
( 040 28 050 0010 1000
) 041 29 051 0010 1001
* 042 2A 052 0010 1010
ASCII Dec Hex Octal Binary
+ 043 2B 053 0010 1011
, 044 2C 054 0010 1100
- 045 2D 055 0010 1101
. 046 2E 056 0010 1110
/ 047 2F 057 0010 1111
0 048 30 060 0011 0000
1 049 31 061 0011 0001
2 050 32 062 0011 0010
3 051 33 063 0011 0011
4 052 34 064 0011 0100
5 053 35 065 0011 0101
6 054 36 066 0011 0110
7 055 37 067 0011 0111
8 056 38 070 0011 1000
9 057 39 071 0011 1001
: 058 3A 072 0011 1010
; 059 3B 073 0011 1011
a 060 3C 074 0011 1100
= 061 3D 075 0011 1101
> 062 3E 076 0011 1110
? 063 3F 077 0011 1111
@ 064 40 100 0100 0000
A 065 41 101 010 00001
B 066 42 102 0100 0010
C 067 43 103 0100 0011
D 068 44 104 0100 0100
E 069 45 105 0100 0101
F 070 46 106 0100 0110
G 071 47 107 0100 0111
H 072 48 110 0100 1000
I 073 49 111 0100 1001
J 074 4A 112 0100 1010
K 075 4B 113 0100 1011
L 076 4C 114 0100 1100
M 077 4D 115 0100 1101
N 078 4E 116 0100 1110
O 079 4F 117 0100 1111
P 080 50 120 010 10000
Q 081 51 121 0101 0001
R 082 52 122 0101 0010
S 083 53 123 0101 0011
T 084 54 124 0101 0100
U 085 55 125 0101 0101
ASCII Dec Hex Octal Binary
V 086 56 126 0101 0110
W 087 57 127 0101 0111
X 088 58 130 0101 1000
Y 089 59 131 0101 1001
Z 090 5A 132 0101 1010
[ 091 5B 133 0101 1011
\ 092 5C 134 0101 1100
] 093 5D 135 0101 1101
^ 094 5E 136 0101 1110
_ 095 5F 137 0101 1111
` 096 60 140 0110 0000
a 097 61 141 0110 0001
b 098 62 142 0110 0010
c 099 63 143 0110 0011
d 100 64 144 0110 0100
e 101 65 145 0110 0101
f 102 66 146 0110 0110
g 103 67 147 0110 0111
h 104 68 150 0110 1000
i 105 69 151 0110 1001
j 106 6A 152 0110 1010
k 107 6B 153 0110 1011
l 108 6C 154 0110 1100
m 109 6D 155 0110 1101
n 110 6E 156 0110 1110
o 111 6F 157 0110 1111
p 112 70 160 0111 0000
q 113 71 161 0111 0001
r 114 72 162 0111 0010
s 115 73 163 0111 0011
t 116 74 164 0111 0100
u 117 75 165 0111 0101
v 118 76 166 0111 0110
w 119 77 167 0111 0111
x 120 78 170 0111 1000
y 121 79 171 0111 1001
z 122 7A 172 0111 1010
{ 123 7B 173 0111 1011
| 124 7C 174 0111 1100
} 125 7D 175 0111 1101
~ 126 7E 176 0111 1110
DEL 127 7F 177 0111 1111

Top.