(a) Primitive data types, integer, real/floating point, character, string and Boolean.
(b) Represent positive integers in binary.
(c) Use of sign and magnitude and two’s complement to represent negative numbers in binary.
(d) Addition and subtraction of binary integers.
(e) Represent positive integers in hexadecimal.
(f) Convert positive integers between binary hexadecimal and denary.
(g) Representation and normalisation of floating point numbers in binary.
(h) Floating point arithmetic, positive and negative numbers, addition and subtraction.
(i) Bitwise manipulation and masks: shifts, combining with AND, OR, and XOR.
(j) How character sets (ASCII and UNICODE) are used to represent text.
A) Primitive Data Types
Primitive data types: the most basic forms of data within programming languages, used for data manipulation
Integer
An integer is a whole number. Integers include zero and negative numbers, they just can’t have a fractional part. Integers are useful for counting things.
6 47238 -12 0 15
Real / Float
Real numbers are positive or negative numbers which can, but do not necessarily, have a fractional part. Reals are useful for measuring things. All integers are real numbers. Real numbers can also be represented using floating point, which is explained later.
0 -71.5 5.01 -80.8 15 .0
Character
A character is a single symbol used by a computer. These include the letters A to Z, the numbers 0 to 9 and hundreds of symbols like %, £ and ,
R { 7 Σ @ s
String
A string is a collection of characters. While a string can be used to store a single character, they can also be used to store many characters in succession. Strings are useful for storing text and phone numbers which start with a 0, which numeric data types like integers would cut off. "Hello, world!" "0778"
Boolean
Named after the mathematician George Boole (hence written with a capital B), values taken by a Boolean data type are restricted to True and False. Booleans are useful for recording data that can only take two values, like the state of a power button or whether a line of code has been executed.
True False
B) Representing Positive In Binary
Computers use switches (transistors) to store and process data and these switches have just two states, either 1 (on) or 0 (off). 1 and 0 are the two numbers in the binary number system.
This means that, in a computer, all data – numbers, characters, sounds, and images – are represented in binary.
Denary is a base-10 number system whereas binary is a base-2 number system.
When we write a number in binary, we use a similar approach to writing numbers in denary but the column headings are based on 2 rather than 1
Denary
A term that describes the number system based on 10, often called decimal.
Starting on the right with the 1s column, as we move left through the columns the place value of each column is multiplied by 2 each time:
The conversion from binary to denary is straightforward: add the column values together for every column containing a 1 in the binary number.
Worked example The binary number 1101101 in denary.
We write down the column value for each column with a 1 in it then add them up. 64 + 32 + 8 + 4 + 1 = 109
To convert an integer from denary to binary:
For each column value, starting with the most significant bit (MSB), we decide if it is smaller than or equal to our decimal number.
If it is smaller or equal to, we record 1 in the table and subtract that column value from the original decimal number. We then check if that new number is smaller than the next column value.
If it is not smaller, we check the new decimal number against the next column and so on.
The bit position in a binary number having the greatest value, or the left-most bit. For an 8-bit binary number this will be the left-most bit with value 128.
Worked example To convert 97 into binary: Is 128 smaller than 97? No, so we record 0 in the 128 column.
Is 64 smaller than 97? Yes, so we record 1 in the 64 column and subtract 64 from 97.
The new number is 97 − 64 = 33
Is 32 smaller than 33? Yes, so we record 1 in the 32 column and subtract 32 from 33.
We now check against 16, 8, 4 and 2 and record 0 in each of those columns.
We are left with 1 so we record 1 in the 1 column.
Task
Complete the Try your self worksheet (CLICK HERE)
C) Use of sign and magnitude and two’s complement
Sign and magnitude This approach follows the convention we use to represent negative denary numbers: a sign, ‘+’ or ‘−’ followed by the number (or magnitude)
Refers to the size of a value and does not take into account the sign, for example the magnitude of both 15 and −15 is 15.
We have to use the MSB to indicate the sign:
‘0’ represents a ‘+’ (positive number)
‘1’ represents a ‘−’ (negative number)
This changes the headings for our columns in an 8-bit binary number:
Worked example Write −29 and +33 in denary in sign and magnitude binary.
To store −29 as a sign and magnitude binary number in 8 bits:
Set the sign bit to 1 to represent ‘−’ (negative number)
store the binary equivalent of 29 in the remaining 7 bits.
To store + 33 as a sign and magnitude binary number in 8 bits:
Set the sign bit to 0 to represent ‘+’ (positive number)
store the binary equivalent of 33 in the remaining 7 bits.
Task
Complete the Try your self worksheet (CLICK HERE)
Two’s complement
While we are used to dealing with a sign and a magnitude, the processing to handle this is quite complex for a computer.
A more effective approach is the two’s complement.
In the two’s complement approach, instead of storing a sign bit in the MSB, we store a negative value equivalent to the magnitude of that column. In 8 bits the MSB would be equivalent to −128.
This changes the headings for our columns in an 8-bit binary number to:
Worked example
Write −37 in denary in two’s complement binary.
To store −37, we store a 1 in the MSB, representing −128, then we add the positive value to represent the difference between −128 and −37 into the remaining 7 bits.
128 − 37 = 91
91 in binary is 1011011
hence, −37 in two’s complement
To store a positive integer we put a 0 in the MSB and store the number in the remaining 7 bits.
Worked example
55 stored in 8 bits in two’s complement form:
There is a method for converting a negative integer into two’s complement form.
For an 8-bit number:
Write down the magnitude of the number in binary (including leading 0s)
Change all the 0s to1s and all the 1s to 0s (1’s complement) (flip the 1s and 0s)
then add 1.
Worked example To convert −87 to two’s complement form in 8 bits
Now check the answer: −128 + 32 + 8 + 1 = −87
So, −87 in two’s complement in 8 bits is: 10101001
Task
Complete the Try your self worksheet (CLICK HERE)
D) Binary Addition / Subtraction
Computers can store whole numbers using binary. Just like humans count in base 10, computers count in base 2, where each step in a place represents a value of two times the previous place. A single binary digit is called a bit, and eight binary digits can be combined to form a byte. Entertainingly, half a byte (four bits) is called a nibble.
TASK
Watch the video for an explanation of how to convert Binary to Decimal and vice versa. The video also demonstrates adding binary numbers.
Answer the worksheet question (CLICK HERE) (you will need to make a copy if you have not been given a copy in your Google Classroom).
Create a poster about positive binary numbers, conversions, and adding binary numbers (CLICK HERE) for blank Google Draw - You will need to make a copy if you have not been provided a copy in Google Classrooms.
Subtraction
Subtracting numbers is also similar to the way we subtract in denary. With base 10 if the number we are subtracting from is smaller than the number we are subtracting, we borrow 10 from the next column.
Worked example What is 135 − 47?
In binary subtractions we use exactly the same approach but, if required, we borrow 10 from the next column, remembering 10 in binary is 2 in denary.
Worked example What is 46 − 11 in binary?
46 in binary is 101110
11 in binary is 1011
Task
Complete the Try your self worksheet (CLICK HERE)
e) Represent positive integers in hexadecimal.
Hexadecimal (hex) is a base 16 number system, commonly used by programmers working with low-level code or with codes for various types of data, for example when coding in HTML the code for the colour orange is #FFA500 or white is #FFFFFF
often use the shorter form to describe hexadecimal.
Computers do not work in hexadecimal but it provides a shorthand for binary that is simpler to understand and remember.
For example, it is hard to remember and easy to make a mistake entering the binary number 11001110001110001101.
However the hexadecimal equivalent, CE38D, is much easier to remember and less likely to be entered incorrectly.
Hexadecimal is convenient because the base is 16 and 16 = 24. This means one hexadecimal digit can represent a 4-bit binary number, making direct conversion between binary and hexadecimal easy.
Hexadecimal is based on 16 and we therefore need 16 symbols to represent the possible values in each column. We use 0−9 for the first ten then A, B, C, D, E and F to represent 10, 11, 12, 13, 14 and 15.
Converting from binary to hexadecimal simply requires each group of 4 bits to be replaced by the equivalent hexadecimal symbol.
Converting from hexadecimal to binary, each symbol is replaced by the corresponding group of 4 bits.
For example, B7D in hexadecimal is 1011 0111 1101 in binary.
f) Convert positive integers between binary hexadecimal and denary
Converting hexadecimal to denary To convert a hex number to its denary equivalent, we multiply each hex digit by its column value.
Worked example Converting B7D in hex into denary:
So B7D in denary is 11 * 256 + 7 * 16 + 12 = 2941
Task
Complete the Try your self worksheet (CLICK HERE)
g) Representation and normalisation of floating-point numbers in binary.
Floating point is a similar concept to standard form as used in science and mathematics.
In standard form, we represent numbers as a decimal fraction raised to a power of 10.
For example, 1.236 * 102 represents123.6.
The decimal point ‘floats’ two places to the right in the number to convert from standard form to standard decimal form.
In floating-point we use a binary fraction multiplied by a power of 2
Worked example Consider the binary floating-point number 0.101 * 2^3 To write this without the exponent, we float the binary point three places to the right to get 101.0 Which is 5 in denary.
In floating-point representation, we use a mantissa, the fractional representation of the number, and an exponent, the binary power to which the number must be raised.
Mantissa The part of the floating-point number that represents the significant digits of the number.
The power of two to which the number in the mantissa must be raised; the number of places the binary point should be moved.
The mantissa is a binary fraction stored in two’s complement form. The MSB is −1 and the remaining bits are positive fractional values starting at ½ and divided by 2 for each column as we move right.
The exponent is a two’s complement integer. For example, with a 6-bit exponent it would be:
Worked example Using a 10-bit mantissa and a 6-bit exponent:
The binary floating-point number 0110100000 000010 Is the same as 0.110100000 * 2^2
The binary point is after the first digit in the mantissa in floating-point form.
Move the binary point 2 places to the right to get