Section Six: Data types Flashcards
Chapter 28 - Primitive data types, binary and hexadecimal
Primitive data types
A primitive data type is one which is provided by a programming language. They include:
- Integer a whole number such as -25, 0, 3, 28679
- Real/float a number with a fractional part such as -13.5, 0.0, 3.142, 100.0001
- Boolean a Boolean variable can only take the value TRUE or FALSE
- Character a letter, number or special character typically represented in ASCII, such as a, A, 4, ? or %. Note that the character “4” is represented differently in the computer from the integer 4 or the real number 4.0
- String anything enclosed in quote marks is a string, for example “Peter”, “123”, or “This is a string”. Either single or double quotes are acceptable.
All data types are held in the computer in binary.
Chapter 28 - Primitive data types, binary and hexadecimal
Number bases
Our familiar decimal (or denary) number system uses the numbers 0 through 9 and therefore has a base of 10. Binary uses only the numbers 0 and 1 and has a base of 2. Hexadecimal uses a base of 16 with numbers 0-9 and letters A to F. A number’s base can be written as a subscript to denote its value in the correct number system. For example 1110 denotes the number eleven in denary. 112 would denote a binary value, (with a denary equivalent of three) and 1116 would denote a hexadecimal value. (17 in denary.)
Chapter 28 - Primitive data types, binary and hexadecimal
The binary number system
In order to better understand the simplicity of the binary number system, it is a good idea to examine how our familiar denary number system works. Columns, right-to-left, represent units, tens and hundreds etc. We mentally multiply the values with their column value and add the totals together.
The principle is exactly the same in the binary number system. As we move from left to right, each digit is worth twice as much as the previous one, instead of ten times as much.
The minimum and maximum values that can be represented in n bits using unsigned binary are 0 and 2n – 1 respectively.
Chapter 28 - Primitive data types, binary and hexadecimal
Converting from denary to binary
To convert a denary number to binary, first write headings of 1, 2, 4, 8 … 128 from right to left. (If the number is greater than 255, continue writing headings.) To convert a denary number, for example 73, into binary, write a 1 under the largest heading less than 73 (i.e. 64). You now have 73 – 64 = 9 remaining, to be converted to binary. 9 = 8 + 1 so put 1 under 8 and under 1. Fill the spaces with zeros. The binary number representing 73 is 01001001.
Chapter 28 - Primitive data types, binary and hexadecimal
The hexadecimal number system
Denary Hexadecimal Binary
0 = 0 = 0
1 = 1 = 1
2 = 2 = 10
3 = 3 = 11
4 = 4 = 100
5 = 5 = 101
6 = 6 = 110
7 = 7 = 111
8 = 8 = 1000
9 = 9 = 1001
10 = A = 1010
11 = B = 1011
12 = C = 1100
13 = D = 1101
14 = E = 1110
15 = F = 1111
16 = 10 = 10000
Chapter 28 - Primitive data types, binary and hexadecimal
Converting from binary to hexadecimal and vice versa
To convert a binary number to hexadecimal, split the binary number into groups of 4 binary digits.
Binary 0011 1010 1111 1010
Hex 3 A F 9
The hex representation of 0011 1010 1111 1010 is therefore 3AF9.
To convert from hex to binary, perform this operation in reverse by grouping the bits in groups of 4 and translating each group into binary. For example, to convert the number 2316 to binary:
Hex 2 3
Binary 0010 0011 = 00100011
Chapter 28 - Primitive data types, binary and hexadecimal
Converting from hexadecimal to denary and vice versa
To convert from hexadecimal to denary, remember that the left column now represents 16s and not tens. For example, to convert 2716 to denary:
16s 1s
Hex = 2 7= 2 x 16 + 7 = 39
To convert a denary number to hex, the easiest way is to first convert the denary number to binary and then translate from binary to hex. For example, to convert 7510 to hex:
128 64 32 16 8 4 2 1
Binary = 0 1 0 0 1 0 1 1
Hex = 4 B
Chapter 28 - Primitive data types, binary and hexadecimal
Why the hexadecimal number system is used?
The hexadecimal system is used as a shorthand for binary since it is simple to represent a byte in just two digits, and fewer mistakes are likely to be made in writing a hex number than a string of binary digits. It is easier for technicians and computer users to write or remember a hex number than a binary number. Colour codes in images often use hexadecimal to represent the RGB values, as they are much easier to remember than a 24-bit binary string. In the example overleaf #364DB2 represents 3616 for Red, 4D16 for Green and B216 for Blue values, which can be displayed or printed in the Colour Picker window more compactly than in binary.
Chapter 29 - ASCII and Unicode
Bits and bytes
A bit is the fundamental unit of information in the form of either a single 1 or 0. 1 and 0 are used to represent the two electronic states: on and off, or more accurately a switch that is closed (to complete a circuit) or open (to break it). A byte is a set of eight bits, for example 0110 1101. One byte holds one character of text.
The number of values that can be represented with n bits is 2n. Two bits can represent 4 different values: 00, 01, 10 and 11. Three bits can represent 8 values and four bits can represent 16 different values, since 2 x 2 x 2 x 2 = 16.
Chapter 29 - ASCII and Unicode
Character form of a denary digit
Although numbers are represented within the code, the number character is not the same as the actual number value. The ASCII value 0110111 will print the character ‘7’, even though the same binary value equates to the denary number 55. Therefore ASCII cannot be used for arithmetic and would use unnecessary space to store numbers. Numbers for arithmetic are stored as pure binary numbers. ‘7’ + ‘7’ (i.e. 0110111 + 0110111 in ASCII) would be 77, not 14 or 110.
Chapter 29 - ASCII and Unicode
The development of ASCII
ASCII originally used only 7 bits, but an 8-bit version was developed to include an additional 128 combinations to represent symbols such as æ, © and ƒ. You can try holding down the ALT key and typing in the code number using the number pad to type one of these symbols. For example, ALT+130 will produce é, as used in café. The 7-bit ASCII code is compatible with the 8-bit code and simply adds a leading 0 to all binary codes.
Chapter 29 - ASCII and Unicode
Unicode
By the 1980s, several coding systems had been introduced all over the world that were all incompatible with one another. This created difficultly as multilingual data was being increasingly used and a new, unified format was sought. As a result, a new 16-bit code called Unicode (UTF-16) was introduced. This allowed for 65,536 different combinations and could therefore represent alphabets from dozens of languages including Latin, Greek, Arabic and Cyrillic alphabets. The first 128 codes were the same as ASCII so compatibility was retained. A further version of Unicode called UTF-32 was also developed to include just over a million characters, and this was more than enough to handle most of the characters from all languages, including Chinese and Japanese.
This meant that whilst there is now just one globally recognised system to maintain, one character in this scheme uses four bytes instead of two, significantly increasing file sizes and data transmission times.
Chapter 30 - Binary arithmetic
Binary addition
Binary addition works in a similar way to denary addition. If two numbers added together are equal to or greater than the base value, (in the case of denary, 10) then the ‘tens’ are carried. In binary, an addition that equals 2 or more results in a carry over to the next column.
In binary, the rules for addition are as follows:
1. 0 + 0 = 0
2. 0 + 1 = 1
3. 1 + 0 = 1
4. 1 + 1 = 0 Carry 1 (This is 2 in denary or 10 in binary.)
5. 1 + 1 + 1 = 1 Carry 1 (This is 3 in denary or 11 in binary.)
Chapter 30 - Binary arithmetic
Overflow
In the following example, 8 bits are used to store the result of an addition. The result of the addition is greater than 255, and an overflow error occurs where a carry from the most significant bit requires a ninth bit.
Chapter 30 - Binary arithmetic
Representing negative numbers using sign and magnitude
One way to represent negative numbers is to make the leftmost bit, called the most significant bit, a sign bit.
- If the most significant bit is zero, the number is positive
- if the most significant bit is one, the number is negative.
In essence we are coding a plus sign as 0 and a minus sign as 1. This is known as the sign and magnitude representation of binary numbers. For example, using one-byte numbers,
00000011 = 3
10000011 = -3
Binary arithmetic using the sign and magnitude representation does not work as you would expect. A much better way of representing numbers in binary is called two’s complement.