0% found this document useful (0 votes)
5 views32 pages

02 CSC01A1 2022 NumeracyForComputerScientists

Yuu

Uploaded by

annieruzeduka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views32 pages

02 CSC01A1 2022 NumeracyForComputerScientists

Yuu

Uploaded by

annieruzeduka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Number systems Number systems issues Exercises

Numeracy for Computer Science

Duncan Coulter1
1
[email protected]

Computer Science 1A

Numeracy for Computer Science


1 / 32
Number systems Number systems issues Exercises

Overview

1 Number systems
Binary
Hexadecimal
Octal

2 Number systems issues

3 Exercises

Numeracy for Computer Science


2 / 32
Number systems Number systems issues Exercises

Numbers as a representation

At some point during the development of human thought an


important abstraction was developed namely the separation of
quantity from the collection of objects themselves.
Different numerals same quantity
Unary: IIIII
Roman numerals: V

Eastern Arabic numerals:


Western Arabic numerals: 5

Numeracy for Computer Science


3 / 32
Number systems Number systems issues Exercises

Modern number representations

Unlike the Roman number system modern representations are


positional (the location of a digit within a number affects the
magnitude of that digit)
Some positional number systems
Decimal: powers of 10.
Binary: powers of 2
Octal: powers of 8
Hexadecimal: powers of 16

Numeracy for Computer Science


4 / 32
Number systems Number systems issues Exercises

The base (radix)

Each positional numbering system is defined by its base (the


number of unique digits available)
The larger the base, the more digits are required
Base 10: 0,1,2,3,4,5,6,7,8,9
Base 2: 0,1
Base 8: 0,1,2,3,4,5,6,7
Base 16: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Numeracy for Computer Science


5 / 32
Number systems Number systems issues Exercises

Range of numbers

The more unique digits a number system has the greater the
range of values it can represent in a finite space.
Calculating the range
r = bp where:
r represents the range (number of expressible values)
b represents the base
p represents the length of the number (number of positions)

Numeracy for Computer Science


6 / 32
Number systems Number systems issues Exercises

The binary numeral system

In classical (non–quantum) computing data are represented by


way of the presence or absence of an observable state. This
leads naturally to the use of a base-2 numbering system (the
binary system). A base-2 numbering system will have two
symbols (e.g. 0 & 1)
State representation in classical computation via the
presence or absence of...
indentations in punched tape reels
electrical charge in Random Access Memory
magnetic charge in Hard Disk Drives
opacity in optical storage

Numeracy for Computer Science


7 / 32
Number systems Number systems issues Exercises

Representing numbers in binary

The binary numbering system (like the decimal system) is a


positional one. Consider for example the number 2910 and its
representation in binary 111012 .
Binary to decimal conversion
Consider how 12310 = 1x102 + 2x101 + 3x100
Likewise

111012 = 1x24 + 1x23 + 1x22 + 0x21 + 1x20


= 1610 + 810 + 410 + 010 + 110
= 2910

Numeracy for Computer Science


8 / 32
Number systems Number systems issues Exercises

Representing numbers in binary II

Just as multiplication was used in converting from binary to


decimal; conversion in the opposite direction will make use of
integer division
Decimal to binary conversion
Remember that 2910 = 111012
Conversion will rely on integer division coupled with modulo
division. In both cases the divisor is 2
Dividend Remainder
29 ...
14 1
7 0
3 1
1 1
0 1

Numeracy for Computer Science


9 / 32
Number systems Number systems issues Exercises

Relationship between binary and boolean logic

There is a strong correspondence between binary numbers and


boolean logic when one considers 12 analogous to true and
02 analogous to false.
Boolean operations
NOT(111012 ) = 000102
AND
1 1 1 0 1
1 0 1 1 1
1 0 1 0 1
OR
1 1 1 0 1
1 0 1 1 1
1 1 1 1 1
111012 XOR 101112 = 010102
Numeracy for Computer Science
10 / 32
Number systems Number systems issues Exercises

Precedence of Boolean Operators

In the same way as BODMAS determines operator precedence


in arithmetic so to do boolean operators have precedence over
each other
Boolean Operator Precedence
1 NOT
2 AND
3 XOR
4 OR

Numeracy for Computer Science


11 / 32
Number systems Number systems issues Exercises

Boolean Logic Rules

The boolean operators are associative, distributive and


commutative. In addition a special rule known as DeMorgan’s
Law needs to be observed during negation.
Rules for Boolean Operators
Associativity: A OR (B OR C) = (A OR B) OR C
Distributivity: A AND (B OR C) = (A AND B) OR (A AND C)
Commutativity: A OR B = B OR A

DeMorgan’s Law
NOT(A OR B) = NOT(A) AND NOT(B)
NOT(A AND B) = NOT(A) OR NOT(B)

Numeracy for Computer Science


12 / 32
Number systems Number systems issues Exercises

Digital Logical Gates

Logical circuits are illustrated using the following notation.

Numeracy for Computer Science


13 / 32
Number systems Number systems issues Exercises

Digital Logical Circuits Example


Consider the following example: NOT (A OR B) AND (C OR D)

Numeracy for Computer Science


14 / 32
Number systems Number systems issues Exercises

Binary addition

Binary addition
0+0=0
0+1=1
1+0=1
1 + 1 = 0 carry 1 to next most significant bit (next position)

Numeracy for Computer Science


15 / 32
Number systems Number systems issues Exercises

Binary addition using boolean logic

Computers perform addition through the bitwise application of


XOR (to determine the output) as well as AND (to determine
the carry bit)
Example
1 1 1 1 1 0 0 0
0 1 1 0 1 1 0 1
0 0 0 1 0 1 1 0
1 0 0 0 0 0 1 1

Numeracy for Computer Science


16 / 32
Number systems Number systems issues Exercises

Binary subtraction

Binary subtraction
0−0=0
0 − 1 = 1 borrow 1 from next most significant bit (next position)
1−0=1
1−1=0

Numeracy for Computer Science


17 / 32
Number systems Number systems issues Exercises

Binary multiplication

Binary multiplication
0∗0=0
0∗1=0
1∗0=0
1∗1=1

Numeracy for Computer Science


18 / 32
Number systems Number systems issues Exercises

Binary multiplication

Computers perform multiplication through the bitwise


application of left shift « as well as AND or addition. Shifting a
number to the left multiplies its value by the base while shifting
to the right divides its value by the base. Repeated addition will
also produce correct results.
Example
0 0 0 0 1 0 1 0 0 1
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 1 0
0 0 1 0 1 0 0 1 0 0
0 0 1 1 1 1 0 1 1 0

Numeracy for Computer Science


19 / 32
Number systems Number systems issues Exercises

Binary division

Division may be performed by repeated subtraction.

Numeracy for Computer Science


20 / 32
Number systems Number systems issues Exercises

Grouping bits

A single 1 or 0 in a binary number is referred to as a bit. In


order to meaningfully work with sequences of 1s and 0s we
need to group them into fixed size groups
Grouping the bits
A group of 8 bits is often called a byte or an octet
A group of 4 bits is sometimes called a nybble.
Each computer architecture has a specified number of bits with
which it works in a single operation. This number is called the
word size for the architecture.
The number of bits used affects:
Accuracy of results
Range of values

Numeracy for Computer Science


21 / 32
Number systems Number systems issues Exercises

Representing negative numbers in binary

In order to represent negative binary numbers one of the bits in


the number (usually the most significant) is designated as the
sign bit. Under this sign and magnitude system 000111012
would represent 2910 while 100111012 would represent -29.
Unfortunately under this scheme extra work needs to be
performed by the computer in order to ensure that addition
between signed numbers operates correctly. In order to
address this shortcoming the two’s complement system is
used
Two’s complement method (8-bit) algorithm
Consider the number −2910
Negate the absolute number NOT(000111012 ) = 111000102
(one’s complement)
Add one: 111000112
Numeracy for Computer Science
22 / 32
Number systems Number systems issues Exercises

Representing negative numbers in binary

Two’s complement revolves around the fact that sign does not
need to be handled separately. What is a negative number? A
number that happens to be less than 0. What is special about
0? Can a new zero be selected?
Two methods for representing negative numbers without
an explicit sign
Radix method (use the base of the number system)
Diminished radix method (use the base of the number system -
1)

Numeracy for Computer Science


23 / 32
Number systems Number systems issues Exercises

A base 10 version of the diminished radix representation

Nine’s complement
Choose a number of digits (word size) e.g. 3
Take 500 as the dividing point between positive and negative
Diminished radix is therefore 10 − 1 = 9
Basis for the number system is therefore 999
Taking the complement of a number is subtracting it from the
standard basis in this case 999 e.g. 999 − 100 = 899
If a number is between 0 and 499 it is positive if it is over 500 it is
a negative number.
To calculate the magnitude of a negative number take its
complement e.g. 910: 999 − 910 = 89
Complementing twice: basis - (basis - value) = value

Numeracy for Computer Science


24 / 32
Number systems Number systems issues Exercises

A base 10 version of radix representation

10’s complement
Adding a negative number should always be the same as
performing subtraction
In Nine’s complement this is not the case (if the sum crosses the
modulus i.e. our new zero value)
In Nine’s complement there are two legal representations of zero
000 and 999 (positive zero and negative zero)
Ten’s complement only contains a single zero
Calculating the complement is simple it is the same as the
diminished radix + 1. So The Ten’s complement of 723 is
1000 − 723 = 277

Numeracy for Computer Science


25 / 32
Number systems Number systems issues Exercises

The consequence of binary fractions

Decimal places in binary numbers represent increasing powers


of 12 . Conversion of a decimal fraction into binary involves
repeatedly multiplying the number by 2, storing a 1 if the result
is at least 1 and 0 otherwise, then discarding any whole
numbers generated and repeating the process until there is no
longer a fractional component. Unfortunately there are many
1
numbers such as 10 which result in repeating binary
representations. Observe the following...
double a = 0.0;
a += 0.1; a += 0.1; a += 0.1; a += 0.1; a += 0.1;
a += 0.1; a += 0.1; a += 0.1; a += 0.1; a += 0.1;
bool b1 = (a == 1); //false
bool b2 = (0.1 * 10 == 1); //false

Numeracy for Computer Science


26 / 32
Number systems Number systems issues Exercises

Other useful number systems

Binary numbers can be cumbersome to work with directly. For


this reason other number systems with bases in powers of 2
are used as shorthand for binary. Most notably Hexadecimal
(base 16) and Octal (base 8)
Hexadecimal
Characters 0–9 followed by A–F
Conversion to decimal:
BEEF16 = 11x163 + 14x162 + 14x161 + 15
To binary: Expand each digit into a 4-bit binary number.
E116 = 1110 00012
From binary: Starting at the left hand side, place numbers into
goups of four bits and convert those each to their equivalent
hexadecimal character. 1011 1110 1110 11112 = BEEF16

Numeracy for Computer Science


27 / 32
Number systems Number systems issues Exercises

Other useful number systems

Octal
Uses the characters 0–7
Conversion to decimal: 1718 = 1(82 ) + 7(81 ) + 1(80 )
From binary: As with hexadecimal simply place into groups of
three bits. 1101112 = 678

Numeracy for Computer Science


28 / 32
Number systems Number systems issues Exercises

Other useful number systems

Binary Coded Decimal


Convert each digit of a decimal number to its binary equivalent.
Conversion to BCD: 12310 = 0001 0010 0011

Numeracy for Computer Science


29 / 32
Number systems Number systems issues Exercises

Binary Coded Decimal Range

Copyright 2010 John Wiley & Sons, Inc

Numeracy for Computer Science


30 / 32
Number systems Number systems issues Exercises

Packed Decimal Representation

Packed Decimal Representation


BCD does not represent digits in the range 1010 to 1510 in a byte
The binary patterns for those digits can be used to represent
sign

Numeracy for Computer Science


31 / 32
Number systems Number systems issues Exercises

Classwork

Complete the following


Convert the following to decimal: 100112
Convert the following to hexadecimal: 101100112
Convert the following to binary: 4310
Convert the following to octal: 1110002
Convert the following to binary: FACE16

Numeracy for Computer Science


32 / 32

You might also like