Section Six: Data types Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Chapter 28 - Primitive data types, binary and hexadecimal
Primitive data types

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Chapter 28 - Primitive data types, binary and hexadecimal
Number bases

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Chapter 28 - Primitive data types, binary and hexadecimal
The binary number system

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Chapter 28 - Primitive data types, binary and hexadecimal
Converting from denary to binary

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Chapter 28 - Primitive data types, binary and hexadecimal
The hexadecimal number system

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Chapter 28 - Primitive data types, binary and hexadecimal
Converting from binary to hexadecimal and vice versa

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Chapter 28 - Primitive data types, binary and hexadecimal
Converting from hexadecimal to denary and vice versa

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Chapter 28 - Primitive data types, binary and hexadecimal
Why the hexadecimal number system is used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Chapter 29 - ASCII and Unicode
Bits and bytes

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Chapter 29 - ASCII and Unicode
Character form of a denary digit

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Chapter 29 - ASCII and Unicode
The development of ASCII

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Chapter 29 - ASCII and Unicode
Unicode

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Chapter 30 - Binary arithmetic
Binary addition

A

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.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Chapter 30 - Binary arithmetic
Overflow

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Chapter 30 - Binary arithmetic
Representing negative numbers using sign and magnitude

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Chapter 30 - Binary arithmetic
Representing negative numbers using two’s complement

A

Two’s complement binary works in a similar way to numbers on an analogue counter. Moving the wheel forwards one, will create a reading of 0001; turn back one, and the reading will become 9999. 9999 is interpreted as -1.

In binary:
11111101 = -3
11111110 = -2
11111111 = -1
00000000 = 0
00000001 = 1
00000010 = 2
00000011 = 3

17
Q

Chapter 30 - Binary arithmetic
Calculating the range

A

The range that can be represented with two’s complement using n bits is given by the formula:

-(2(n-1)) … 2(n-1) - 1

With eight bits, the maximum denary range that can be represented is -128 to 127 because the leftmost bit is used as a sign bit to indicate whether a number is negative. If the leftmost number is a 1, it is a negative number. Thus 10000000 represents -128.

18
Q

Chapter 30 - Binary arithmetic
Converting a negative denary number to binary

A

Start by working out the positive equivalent of the number, flip all of the bits and add 1. For example, to convert the denary number -9 to binary:

-9
Positive binary : 00001001
Flip the bits : 11110110
Add one : 1
11110111

19
Q

Chapter 30 - Binary arithmetic
Converting a negative two’s complement binary number to denary

A

The same method works the other way. Flip all of the bits and add 1. Then work out the result in denary using the normal method. For example, to convert the binary number 11100101 to denary:

11100101
Flip the bits : 00011010
Add one : 1
Convert : - 00011011
-27

20
Q

Chapter 30 - Binary arithmetic
Binary subtraction using two’s complement

A

Binary subtraction is best done by using the negative two’s complement number and then adding the second number. For example denary 17-14 would be:
14 = 00001110
-14 = 11110010
17 = 00010001
17 + (-14) = (1) 00000011
The carry on the addition is ignored, and the correct answer is given.

21
Q

Chapter 30 - Binary arithmetic
Fixed point binary numbers

A

Fixed point binary numbers can be a useful way to represent fractions in binary. A binary point is used to separate the whole place values from the fractional part on the number line:

0.0.0.0.0.0.0.0 = 8.4.2.1.1⁄2.1⁄4.1⁄8.1⁄16

In the binary example above, the left hand section before the point is equal to 5 (4+1) and the right hand section is equal to 1⁄2 + 1⁄4 (3⁄4), or 0.5 + 0.25 = 0.75. So, using four bits after the point, 0101 1100 is 5.75 in denary. A useful table with some denary fractions and their equivalents is given below:

Binary fraction Fraction Denary fraction
0.1 = 1/2 = 0.5
0.01 = 1/4 = 0.25
0.001 = 1/8 = 0.125
0.0001 = 1/16 = 0.0625
0.00001 = 1/32 = 0.03125
0.000001 = 1/64 = 0.015625
0.0000001 = 1/128 = 0.0078125
0.00000001 = 1/256 = 0.00390625

22
Q

Chapter 30 - Binary arithmetic
Converting a denary fraction to fixed point binary

A

To convert the fractional part of a denary number to binary, you can employ the same technique as you would when converting any denary number to binary. Take the value and subtract each point value from the amount until you are left with 0. Take the example 3.5625 using 4 bits to the right of the binary point:

Subtract 0.5: 0.5625 – 0.5 = 0.0625 1
Subtract 0.25 from 0.0625: Won’t go 0
Subtract 0.125 from 0.0625: Won’t go 0
Subtract 0.0625 from 0.0625: 0.0625 – 0.0625 = 0 1
3 = 0011 in binary. 0.5625 = 1001. So 3.5625 = 0011 1001

It is worth noticing that this system is not only less accurate than the denary system, but some fractions cannot be represented at all. 0.2, 0.3 and 0.4, for example, will require an infinite number of bits to the right of the point. The number of fractional places would therefore be truncated and the number will not be accurately stored, causing rounding errors. In our denary system, two denary places can hold all values between .00 and .99. With the fixed point binary system, 2 digits after the point can only represent 0, 1⁄4, 1⁄2, or 3⁄4 and nothing in between.

The range of a fixed point binary number is also limited by the fractional part. For example, if you have only 8 bits to store a number to 2 binary places, you would need 2 digits after the point, leaving only 6 bits before it. 6 bits only gives a range of 0-63. Moving the point one to the left to improve accuracy within the fractional part only serves to half the range to just 0 31. Even with 32 bits used for each number, including 8 bits for the fractional part after the point, the maximum value that can be stored is only about 8 million. Another format called floating point binary can hold much larger numbers, with greater accuracy.

23
Q

Chapter 31 – Floating point arithmetic
Fixed point binary numbers

A

In the last chapter we looked briefly at how numbers with a fractional part can be held in fixed point format, which assumes a predetermined number of bits before and after the point. This makes fixed point numbers simpler to process but there is a compromise in the range and precision of values that can be represented in a given number of bits. Moving the point to the right increases the range but reduces the precision, or accuracy, of the fractional part and vice versa.

0.0.0.0.0.0.0.0 = 16.8.4.2.1.1⁄2.1⁄4.1⁄8

24
Q

Chapter 31 – Floating point arithmetic
Floating point binary numbers

A

Using 32 bits (4 bytes), the largest fixed point number that can be represented with just one bit after the point is only just over two billion. Floating point binary allows very large numbers to be represented.

When ordinary denary numbers become very large, they are written in a more convenient scientific notation m × 10n where m is known as the mantissa or coefficient, and n is the exponent or order of magnitude. 5000 can therefore written as 0.5 x 104, and 42,750.254 can be written as 0.42750254 x 105,
moving the decimal point five places to the left.

This technique can easily be applied to binary numbers too, where the mantissa and exponent are represented for example using 12 bits, with 8 bits for the mantissa and 4 bits for the exponent. The leftmost bit of both the mantissa and the exponent is a sign bit, with 0 indicating a positive number, and 1 a negative number. In a computer, of course, many more bits than this will be used to represent a floating point number, with 32-, 64- and 128-bit floating point numbers all being common. In all the examples below, eight bits are used for the mantissa and four bits for the exponent. The implied binary point is to the right of the sign bit.

To convert the floating point binary number above to denary:
- Write down the mantissa, 0.1011010
- Translate the exponent from binary to denary 0011 = 3. This means that you have to move the point 3 places to the right, as the mantissa has to be multiplied by 23.
- The binary number is therefore 101.1010
- Translate this to binary using the table in Figure 31.1. The number is 5.625.

25
Q

Chapter 31 – Floating point arithmetic
Negative exponents

A

If the exponent is negative, the decimal point must be moved left instead of right.
0 * 1000000 1110 = 0.1 x 2-2 = 0.001 = 0.125

The example above has a positive mantissa of 0.1000000 and a negative exponent of -2.
- Find the two’s complement of the exponent. (Remember that to convert a positive to negative binary number using two’s complement you must flip the bits and add 1.) Exponent = -2
- Move the binary point of the mantissa two places to the left, to make it smaller. The mantissa is therefore 0.001 (You can ignore the trailing zeros)
- Translate this to denary with the help of Figure 31.1. The answer is 0.125.

26
Q

Chapter 31 – Floating point arithmetic
Handling negative mantissas

A

A negative floating point number will have a 1 as the sign bit or MSB (Most Significant Bit) of the mantissa indicating a negative place value.
1 * 0101101 0101 = - 0.1010011 x 25 = -10100.11 = - 20.75

The example above has a negative mantissa of 1.0101101 and a positive exponent of 0101.
- Find the twos complement of the mantissa. It is 0.1010011, so the bits represent -0.1010011
- Translate the exponent to denary, 0101 = 5
- Move the binary point 5 places to the right to make it larger. The mantissa is -10100.11
- Translate this to binary with the help of Figure 31.1. The answer is -20.75.

27
Q

Chapter 31 – Floating point arithmetic
Normalisation

A

Normalisation is the process of moving the binary point of a floating point number to provide the maximum level of precision for a given number of bits. This is achieved by ensuring that the first digit after the binary point is a significant digit. To understand this, first consider an example in denary.

In the denary system, a number such as 5,842,13010 can be represented with a 7-digit mantissa in many different ways
0.584213 x 107 = 5,842,130
0.058421 x 108 = 5,842,100
0.005842 x 109 = 5,842,000
The first representation, with a significant (non-zero) digit after the decimal point, has the maximum precision.
A number such as 0.00000584213 can be represented as 0.584213 x 10-5.

28
Q

Chapter 31 – Floating point arithmetic
Normalising a positive binary number

A

In binary arithmetic, the leading bit of both mantissa and exponent represent the sign bit.
In normalised floating point form:

A positive number has a sign bit of 0 and the next digit is always 1.
This means that the mantissa of a positive number in normalised form always lies between 1⁄2 and 1.

29
Q

Chapter 31 – Floating point arithmetic
Normalising a negative binary number

A

An unnormalised number will have a sign bit of 1 and one or more 1s after the binary point.

30
Q

Chapter 31 – Floating point arithmetic
Converting from denary to normalised binary floating point

A

To convert a denary number to normalised binary floating point, first convert the number to fixed point binary.

31
Q

Chapter 31 – Floating point arithmetic
Floating point addition and subtraction

A

Before looking at these operations in binary, we can gain an understanding of the principles involved in floating point arithmetic by looking at equivalent calculations in denary.

In denary, when adding two numbers involving decimal points, we first have to line up the points.

For example:
132.156
+ 1.0318
133.1878
In their “normalised form”, the two numbers above would be represented as
.132156 x 103 and
.103180 x 101
Clearly we do not simply add the mantissas, and the same principle holds true in binary. The rules for addition and subtraction can be stated as:
- line up the points by making the exponents equal
- add or subtract the mantissas
- normalise the result

32
Q

Chapter 31 – Floating point arithmetic
Underflow and overflow

A

Underflow occurs when a number is too small to be represented in the allotted number of bits. If, for example, a very small number is divided by another number greater than 1, underflow may occur and the result will be represented by 0.

Overflow occurs when the result of a calculation is too large to be held in the number of bits allocated.

33
Q

Chapter 32 - Bitwise manipulation and masks
Logical shift instructions

A

All the bits move right or left. A logical shift right causes the least significant bit (lsb) to be shifted into the carry bit, and a zero moves into the most significant bit (msb) to occupy the vacated space.

34
Q

Chapter 32 - Bitwise manipulation and masks
Arithmetic shift instructions

A

An arithmetic shift is similar, but it takes into account the sign bit, which always remains the same.

35
Q

Chapter 32 - Bitwise manipulation and masks
Multiplying two numbers using arithmetic shifts

A

Using a combination of shifts and addition, two binary numbers may be multiplied together.

36
Q

Chapter 32 - Bitwise manipulation and masks
Circular shift instructions

A

A rotate or circular shift is useful for performing shifts in multiple bytes. In a circular shift right, the value in the least significant bit (lsb) is moved into the carry bit, and the carry bit is moved into the most significant bit (msb).

37
Q

Chapter 32 - Bitwise manipulation and masks
Logical instructions

A

Boolean algebra is covered in Section 8, Chapters 40 and 41.
The instructions NOT, AND, OR and XOR (exclusive OR) have the following effects:
NOT AND OR XOR
Input A 1010 1010 1010 1010
Input B 1100 1100 1100
Result 0101 1000 1110 0110

Explanation:
In Boolean logic, 1 represents True and 0 represents False. A NOT instruction has only one input. If the input is True (i.e. 1), the output is False (i.e. 0). With the AND gate, if both inputs are True, (i.e.1) the output is True. Otherwise, the output is False. With the OR gate, if either of the inputs is True (i.e. 1) the output is True. Otherwise, the output is False. With the XOR gate, if either, but not both, of the inputs is True, the output is True. Otherwise, the output is False.

38
Q

Chapter 32 - Bitwise manipulation and masks
Masks

A

The OR function may be used to set selected bits to 1 without affecting the other bits.