02 CSC01A1 2022 NumeracyForComputerScientists
02 CSC01A1 2022 NumeracyForComputerScientists
Duncan Coulter1
1
[email protected]
Computer Science 1A
Overview
1 Number systems
Binary
Hexadecimal
Octal
3 Exercises
Numbers as a representation
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)
DeMorgan’s Law
NOT(A OR B) = NOT(A) AND NOT(B)
NOT(A AND B) = NOT(A) OR NOT(B)
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)
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
Binary multiplication
Binary multiplication
0∗0=0
0∗1=0
1∗0=0
1∗1=1
Binary multiplication
Binary division
Grouping bits
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)
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
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
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
Classwork