0% found this document useful (0 votes)
10 views

Binary Number System

Uploaded by

Florence Dzoro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Binary Number System

Uploaded by

Florence Dzoro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Binary number system

The binary system uses 1s and 0s only which gives these corresponding weightings:

A typical binary number would be:

Converting from binary to denary and from denary to binary


It is fairly straightforward to change a binary number into a denary number. Each time a 1
appears in a column, the column value is added to the total. For example, the binary number
above is:

The 0 values are simply ignored when calculating the total.


The reverse operation – converting from denary to binary – is slightly more complex. There are
two basic ways of doing this.
Consider the conversion of the denary number, 107, into binary …

Method 1
This method involves placing 1s in the appropriate position so that the total equates to 107.

ACTIVITY 1A
Convert these binary numbers into denary.
a) 0 0 1 1 0 0 1 1
b) 0 1 1 1 1 1 1 1
c) 1 0 0 1 1 0 0 1
d) 0 1 1 1 0 1 0 0
e) 1 1 1 1 1 1 1 1
f) 0 0 0 0 1 1 1 1
g) 1 0 0 0 1 1 1 1
h) 0 0 1 1 0 0 1 1
i) 0 1 1 1 0 0 0 0
j) 1 1 1 0 1 1 1 0
Method 2
This method involves successive division by 2; the remainders are then written from bottom to
top to give the binary value.

ACTIVITY 1B
Convert these denary numbers into binary (using either method). a) 4 1
b) 6 7
c) 8 6
d) 1 0 0
e) 1 1 1
f) 1 2 7
g) 1 4 4
h) 1 8 9
i) 2 0 0
j) 2 5 5

Binary addition and subtraction


Up until now we have assumed all binary numbers have positive values. There are a number of
methods to represent both positive and negative numbers. We will consider:
• one’s complement
• two’s complement.
In one’s complement, each digit in the binary number is inverted (in other words, 0 becomes 1
and 1 becomes 0). For example, 0 1 0 1 1 0 1 0 (denary value 90) becomes 1 0 1 0 0 1 0 1
(denary value −90).
In two’s complement, each digit in the binary number is inverted and a ‘1’ is added to the
rightmost bit. For example, 0 1 0 1 1 0 1 0 (denary value 90) becomes:
Throughout the remainder of this chapter, we will use the two’s complement method to avoid
confusion. Also, two’s complement makes binary addition and subtraction more
straightforward. The reader is left to investigate one’s complement and the sign and magnitude
method in binary arithmetic.
Now that we are introducing negative numbers, we need a way to represent these in binary.
The two’s complement uses these weightings for an 8-bit number representation:

This means:

The first example is: −128 + 64 + 16 + 8 + 2 = −38

The easiest way to convert a number into its negative equivalent is to use two’s complement.
For example, 104 in binary is 0 1 1 0 1 0 0 0.
To find the binary value for −104 using two’s complement:

A C T I V I T Y 1C
C onv ert these denary numbers i nto 8-bi t bi nary numbers usi ng two’ s compl ement where
necessary . U se these bi nary col umn wei g hti ng s:

a) + 114
b) + 61
c) + 96
d) − 14
e) − 116

B inar y addi tion


Consider Examples 1.1 and 1.2.

Example 1.1
A dd 0 0 1 0 0 1 0 1 ( 37 i n denary ) and 0 0 1 1 1 0 1 0 ( 58 i n denary ) .
S ol uti on

T hi s gi v es us 0 1 0 1 1 1 1 1, whi ch i s 95 i n denary ; the correct answer.

E x ample 1.2

To carry out subtraction in binary, we convert the number being subtracted into its negative
equivalent using two’s complement, and then add the two numbers.

Example 1.3
C arry out the subtracti on 49 – 80 i n bi nary .
S ol uti on
1 C onv ert the two numbers i nto bi nary :
49 = 0 0 1 1 0 0 0 1
80 = 0 1 0 1 0 0 0 0
2 F i nd the two’ s compl ement of 80:

3 A dd 49 and − 80:
Measurement of the size of computer memories
The byte is the smallest unit of memory in a computer. Some computers use larger bytes, such
as 16-bit systems and 32-bit systems, but they are always multiples of 8. 1 byte of memory
wouldn’t allow you to store very much information; so memory size is measured in these
multiples. See Table 1.1.

Name of memory size Equivalent denary value (bytes)


1 kilobyte (1 KB) 1 000
1 megabyte (1 MB) 1 000 000
1 gigabyte (1 GB) 1 000 000 000
1 terabyte (1 TB) 1 000 000 000 000
1 petabyte (1 PB) 1 000 000 000 000 000
Table 1.1 Memory size and denary values

The system of numbering shown in Table 1.1 only refers to some storage devices, but is
technically inaccurate. It is based on the SI (base 10) system of units where 1 kilo is equal to
1000. A 1 TB hard disk drive would allow the storage of 1 × 10 12 bytes according to this system.
However, since memory size is actually measured in terms of powers of 2, another system has
been proposed by the International Electrotechnical Commission (IEC); it is based on the binary
system. See Table 1.2.

Name of memory size Number of bytes Equivalent denary value (bytes)

1 kibibyte (1 KiB) 210 1 024

1 mebibyte (1 MiB) 220 1 048 576

1 gibibyte (1 GiB) 230 1 073 741 824

1 tebibyte (1 TiB) 240 1 099 511 627 776

1 pebibyte (1 PiB) 250 1 125 899 906 842 624


Table 1.2 IEC memory size system

This system is more accurate. Internal memories (such as RAM) should be measured using the
IEC system. A 64 GiB RAM could, therefore, store 64 × 230 bytes of data (68 719 476 736 bytes).
See Section 1.2 for examples of how to calculate the size of a file.
1.1.3 Hexadecimal number system
The hexadecimal system is very closely related to the binary system. Hexadecimal (sometimes
referred to as simply hex) is a base 16 system with the weightings:

Because it is a system based on 16 different digits, the numbers 0 to 9 and the letters A to F are
used to represent hexadecimal digits.
A = 10, B = 11, C = 12, D = 13, E = 14 and F = 15.

Since 16 = 24, four binary digits are equivalent to each hexadecimal digit. Table 1.3 summarises
the link between binary, hexadecimal and denary.

Binary value Hexadecimal value Denary value


0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15
Table 1.3 The link between binary, hexadecimal and denary

Converting from binary to hexadecimal and from hexadecimal to


binary
Converting from binary to hexadecimal is a fairly easy process. Starting from the right and
moving left, split the binary number into groups of 4 bits. If the last group has less than 4 bits,
then simply fill in with 0s from the left. Take each group of 4 bits and convert it into the
equivalent hexadecimal digit using Table 1.3.
Examples 1.5 and 1.6 show you how this works.

Example 1.5
C onv ert 1 0 1 1 1 1 1 0 0 0 0 1 from bi nary to hex adeci mal .
S ol uti on
F i rst spl i t i t i nto g roups of 4 bi ts:

T hen fi nd the equi v al ent hex adeci mal di g i ts:

E x ample 1.6
ACTIVITY 1E
Convert these binary numbers into hexadecimal.
a) 1 1 0 0 0 0 1 1
b) 1 1 1 1 0 1 1 1
c) 1 0 0 1 1 1 1 1 1 1
d) 1 0 0 1 1 1 0 1 1 1 0
e) 0 0 0 1 1 1 1 0 0 0 0 1
f) 1 0 0 0 1 0 0 1 1 1 1 0
g) 0 0 1 0 0 1 1 1 1 1 1 1 0
h) 0 1 1 1 0 1 0 0 1 1 1 0 0
i) 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1
j) 0 0 1 1 0 0 1 1 1 1 0 1 0 1 1 1 0

Converting from hexadecimal to binary is also straightforward. Using the data from Table 1.3,
simply take each hexadecimal digit and write down the 4 bit code which corresponds to the
digit.

Example 1.7
Convert this hexadecimal number to its binary equivalent.
4 5 A

Solution
Using Table 1.3, find the 4-bit code for each digit:
0100 0101 1010
Put the groups together to form the binary number:
010001011010
Example 1.8
Convert this hexadecimal number to its binary equivalent.
B F 0 8
Solution
Using Table 1.3:
1011 1111 0000 1 0 0 0 Then
put all the digits together:

1011111100001000

Use of the hexadecimal system


This section reviews two uses of the hexadecimal system.

Memory dumps
It is much easier to work with:
B 5 A 4 1 A F C than it is to
work with:

10110101101001000001101011111100
So, hexadecimal is often used when developing new software or when trying to trace errors in
programs. When the memory contents are output to a printer or monitor, this is known as a
memory dump.

ACTIVITY 1F
Convert these hexadecimal numbers into binary. a) 6 C
b) 5 9
c) A A
d) A 0 0
e) 4 0 E
f) B A 6
g) 9 C C
h) 4 0 A A
i) D A 4 7
j) 1 A B 0

A program developer can look at each of the hexadecimal codes (as shown in Table 1.4) and
determine where the error lies. The value on the far left shows the memory location, so it is
possible to find out exactly where in memory the fault occurs. Using hexadecimal is more
manageable than binary. It is a powerful fault-tracing tool, but requires considerable
knowledge of computer architecture to be able to interpret the results.

Table 1.4 Memory dump

1.1.4 Binary-coded decimal (BCD) system


The binary-coded decimal (BCD) system uses a 4-bit code to represent each denary digit:

Therefore, the denary number 3 1 6 5 would be 0 0 1 1 0 0 0 1 0 1 1 0 0 1 0 1 in BCD format.


The 4-bit code can be stored in the computer either as half a byte or two 4-bit codes stored
together to form one byte. For example, using 3 1 6 5 again …

Method 1: four single bytes

Method 2: two bytes


b)

Uses of BCD
The most obvious use of BCD is in the representation of digits on a calculator or clock display.

Each denary digit will have a BCD equivalent value which makes it easy to convert from
computer output to denary display.
As you will learn in Chapter 13, it is nearly impossible to represent decimal values exactly in
computer memories which use the binary number system. Normally this doesn’t cause a major
issue since the differences can be dealt with. However, when it comes to accounting and
representing monetary values in computers, exact values need to be stored to prevent
significant errors from accumulating. Monetary values use a fixed-point notation, for example
$1.31, so one solution is to represent each denary digit as a BCD value.
Consider adding $0.37 and $0.94 together using fixed-point decimals.

Using binary addition, this sum will produce:


0 0 0 0 0 0 0 0 . 1 1 0 0 1 0 1 1 which produces 1 1 0 0 (denary 12) and 1 0 1 1 (denary 11), which
is clearly incorrect. The problem was caused by 3 + 9 = 12 and 7 + 4 = 11, as neither 12 nor 11
are single denary digits. The solution to this problem, enabling the computer to store monetary
values accurately, is to add 0 1 1 0 (denary 6) whenever such a problem arises. The computer
can be programmed to recognise this issue and add 0 1 1 0 at each appropriate point.
If we look at the example again, we can add .07 and .04 (the two digits in the second decimal
place) first.
Now we will add .3 and .9 together (the two digits in the first decimal place) remembering the
carry bit from the addition above:

This produces 1 1 0 1 which isn’t a denary digit; this will flag an error and the computer again
needs to add 0 1 1 0.

Adding 1 to 0 0 0 0 0 0 0 0 produces:
Final answer:

which is 1.31 in denary – the correct answer.

ACTIVITY 1H
Carry out these BCD additions.
a) 0.45 + 0.21
b) 0.66 + 0.51
c) 0.88 + 0.75

1.1.5 ASCII codes and Unicodes


The ASCII code system (American Standard Code for Information Interchange) was set up in
1963 for use in communication systems and computer systems. The newer version of the code
was published in 1986. The standard ASCII code character set consists of 7-bit codes (0 to 127
denary or 0 to 7F in hexadecimal); this represents the letters, numbers and characters found on
a standard keyboard together with 32 control codes (which use up codes 0 to 31 (denary) or 0
to 19 (hexadecimal)).
Table 1.5 shows part of the standard ASCII code table (only the control codes have been
removed from the table).
▲ Table 1.5 Part of the ASCII code table

Notice the storage of characters with uppercase and lowercase. For example:
Notice the sixth bit changes from 1 to 0 when comparing lower and uppercase characters. This
makes the conversion between the two an easy operation. It is also noticeable that the
character sets (such as a to z, 0 to 9, and so on) are grouped together in sequence, which
speeds up usability.
Extended ASCII uses 8-bit codes (128 to 255 in denary or 80 to FF in hex). This allows for
nonEnglish characters and for drawing characters to be included.
Since ASCII code has a number of disadvantages and is unsuitable for some purposes, different
methods of coding have been developed over the years. One coding system is called Unicode.
Unicode allows characters in a code form to represent all languages of the world, thus
supporting many operating systems, search engines and internet browsers used globally. There
is overlap with standard ASCII code, since the first 128 (English) characters are the same, but
Unicode can support several thousand different characters in total. As can be seen in Tables 1.5
and 1.6, ASCII uses one byte to represent a character, whereas Unicode will support up to four
bytes per character.
▲ Table 1.6 Extended ASCII code table

The Unicode consortium was set up in 1991. Version 1.0 was published with five goals, these
were to
• create a universal standard that covered all languages and all writing systems
• produce a more efficient coding system than ASCII
• adopt uniform encoding where each character is encoded as 16-bit or 32-bit code
• create unambiguous encoding where each 16-bit or 32-bit value always represents the same
character (it is worth pointing out here that the ASCII code tables are not standardised and
versions other than the ones shown in tables 1.5 and 1.6 exist)
• reserve part of the code for private use to enable a user to assign codes for their own
characters and symbols (useful for Chinese and Japanese character sets).
A sample of Unicode characters are shown in Table 1.7. As can be seen from the table,
characters used in languages such as Russian, Greek, Romanian and Croatian can now be
represented in a computer).
▲ Table 1.7 Sample of Unicode characters

You might also like