Presentation is loading. Please wait.

Presentation is loading. Please wait.

A-level Computer Science

Similar presentations


Presentation on theme: "A-level Computer Science"— Presentation transcript:

1 A-level Computer Science
1.4.1 – Data Types A-level Computer Science

2 Specification Overview
Specification Points 1.4.1 Data Types Primitive data types, integer, real/floating point, character, string and Boolean. Represent positive integers in binary. Use of sign and magnitude and two’s complement to represent negative numbers in binary. Addition and subtraction of binary integers. Represent positive integers in hexadecimal. Convert positive integers between binary, hexadecimal and denary. Representation and normalisation of floating point numbers in binary. Floating point arithmetic, positive and negative numbers, addition and subtraction. Bitwise manipulation and masks: shifts, combining with AND, OR and XOR. How character sets (ASCII and UNICODE) are used to represent text.

3 Overview Computers use electricity to represent information. An electric current has two possible states: it can either be on or off. Computers represent ‘on’ using the digit 1 and ‘off’ using the digit 0. These 0s and 1s are called binary and combinations of 0s and 1s can be used to represent anything from a simple number to a film. Each 0 or 1 in binary is called a bit (short for binary digit). Because it requires so many bits to represent quite small numbers, let alone complex things like games or music files, groups of bits are commonly grouped together and given names. A group of 4 bits is called a nibble and can represent 24 (128) combinations. A group of 8 bits is called a byte; a byte ( ) can provide 28 (256) different combinations of 0s and 1s. Bytes are grouped together in the following ways: 1 024 bytes = 1 kilobyte (kB) = 210 bytes bytes = 1 megabyte (MB) = 220 bytes bytes = 1 gigabyte (GB) = 230 bytes bytes = 1 terabyte (TB) = 240 bytes

4 Primitive Data Types Nearly all programming languages provide programmers with five primitive (or simple) data types that can be connected together to create more complex (or composite) data types. The five primitive data types are integer, real, character, string and Boolean. The amount of memory that each data type takes up varies from language to language and processor to processor (they take up more bits on a 64-bit system compared to a 32-bit system). A good estimate is that a character takes up 8 bits and an integer 32 bits. The rest of this chapter will show how binary is used to represent all possible values held using these data types.

5 Task Copy this table into your blog and complete: Field/Variable
Data Type Justification for choice of Data Type Shoe Size Password Multiple Choice Answer (Single Letter) Age Over 18? Goal Difference

6 Representing Positive Numbers in Binary
Binary is a base 2 number system, which means that each value is twice as large as the value before it. Our usual number system is base 10, also known as denary. In denary, each digit is ten times larger than the one before it. In the denary system, 1011 means one thousand and eleven, because equals one thousand and eleven. In the binary system 1011 means eleven, because = 11. 1000 100 10 1 8 4 2 1

7 Converting from Denary to Binary
98 in denary into binary 98 divide by 2 = 49 remainder 0 49 divide by 2 = 24 remainder 1 24 divide by 2 = 12 remainder 0 12 divide by 2 = 6 remainder 0 6 divide by 2 = 3 remainder 0 3 divide by 2 = 1 remainder 1 1 divide by 2 = 0 remainder 1 0 divide by 2 = 0 remainder 0 Write the binary value starting from the remainder at the top on the far right hand side. which equals 98

8 Converting from Binary to Denary
= 135 128 64 32 16 8 4 2 1 1 1 1 1

9 Task Convert the following, show your workings: 01111001 into denary
44 into binary in denary 111 into binary into denary

10 Representing negative numbers in binary
Binary can also be used to represent negative numbers (−1, −2, −3 and so on). There are two ways in which this can be done, you’ll need to read the exam questions carefully to make sure you use the correct method. Sign and Magnitude Two’s Complement

11 Sign and Magnitude Sign and magnitude is the simplest way of representing negative numbers in binary. The most significant bit (MSB) is simply used to represent the sign: 1 means the number is negative, 0 means it is positive. So to represent −4 in 8-bit binary you would write out the 4 in binary, , then change the MSB to a 1, creating This notation isn’t used very often as using the MSB as a sign bit means that the largest number that can be represented using 8 bits is 127, much less than 255. It also makes it harder to do calculations as different bits mean different things; some represent numbers, others represent signs. Also, their value of zero is represented twice as both positive and negative 0. MSB notation can represent numbers in the range −127 to 127.

12 Two’s Complement Two’s complement is a much more useful way of representing binary numbers as it allows you to use negative numbers without reducing the range of numbers you can use. The easiest way to show a negative number using two’s complement is to write it out as a positive number using the usual binary method. Then, starting at the right-hand side, leave every bit up to and including the first 1 alone, but invert all the bits after this. UBO3Z2dxnIfuNDspmJmorJB

13 Two’s Complement For example, to show −90 in binary, first work out is because = 90. Then start at the right-hand side and leave everything alone up to and including the first 1: Finally invert everything after this, so all the 1s become 0s and vice versa: Thus, −90 in binary is in two’s complement notation. 128 64 32 16 8 4 2 1 128 64 32 16 8 4 2 1 (-)128 64 32 16 8 4 2 1

14 Binary Addition Binary addition is fairly straightforward. Just remember these simple operations: = = = = 1 0 (1 and 0 / 0 carry 1) = 1 1 (1 and 1 / 1 carry 1)

15 Binary Addition To add together two binary numbers, just arrange them above each other in a table. The following table shows the number 59 above the number 117: So our final result is or 176. We know this is correct because we can check our answer: = 176. 1

16 Task Add together the following binary values, show your workings:

17 Exam Tip! In an exam it can be tempting just to work out the answer in denary and then write down the binary equivalent of this number. Avoid this temptation, as questions of this type may require you to show your working.

18 Binary Subtraction 1 Binary subtraction is the same as binary addition except that you convert the number to be subtracted into negative binary (using two’s complement) before adding them together. For example, if you want to calculate 123 − 77 in binary, the first thing you do is write down the binary for 123. Then you work out what −77 is in binary using two’s complement and write this beneath it (77 is , thus -77 is ) Finally, you add them together using the same operations as for binary addition. (Notice that the final 1 that would be carried off the end of the final (8th) bit is just ignored.) So the answer is = 46 and we know this is correct because 123 − 77 = 46. 1 1

19 Task Subtract the following binary values, show your workings:
103 – 42

20 Representing positive integers in hexadecimal and converting between binary, hexadecimal and denary
Binary can be difficult for people to understand and it’s easy to make mistakes when recording numbers. To get around this problem we often use a base 16 number system, called hexadecimal, to represent numbers in computing. Hex is used in computing… to represent colours using the RGB colour model (#FF00A1) within Mac addressing (00:0a:95:9d:68:16) to express error codes (0x32DD – see us/library/ms681381(VS.85).aspx) You’ll need to know how to convert between denary, binary and hexadecimal. Luckily it isn’t very hard. Hexadecimal uses 16 symbols, the numbers 0–9 and letters A–F.

21 Denary Binary (4-bit) Hexadecimal 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F

22 Converting Binary/Denary to Hexadecimal
To convert a denary number to hexadecimal, simply convert it to 8-bit binary. Then split your 8-bit binary number into two 4-bit sections and convert each of these to hexadecimal using the table above. Then split this 8-bit binary number into two 4-bit sections: E.g. 108 = 1 8 4 2 1 8 4 2 1

23 Converting Binary/Denary to Hexadecimal
Finally convert each of these 4-bit segments to hexadecimal: = 6 (6) = 12 (C) 108 in hex is 6C 1 8 4 2 1 8 4 2 1

24 Converting Hexadecimal to Denary
To convert a hexadecimal number into denary we use the column values just like what we did for binary, but this time for a base 16 numbering system. Thus, A2C as a denary number is… A2C is = 2604 in denary. 4096 256 16 1 163 = 4096 162 = 256 161 = 16 160 = 1 4096 256 16 1 A 2 C 10 x 256 2 x 16 12 x 1

25 Task Convert the following numbers into Hex: 11110101 01010101 87 131
Convert the following Hex values into denary: A3 69 8AF 1C2E

26 Floating Point Numbers in Binary
Floating point binary is used to hold really big, really small or really accurate numbers using just 8 bits. In GCSE Mathematics you learned about floating point notation (sometimes called scientific notation). Using floating point, the number 92 can be represented as 0.92 × 100, which is the same as 0.92 × 102. It is this last part that we refer to as floating point notation. It is called floating point because the number of digits is fixed, but the decimal point floats around.

27 Floating Point Numbers in Binary
Using the example 0.92 × 102, 0.92 is the mantissa, 10 is the base and 2 is the exponent. The mantissa is the part of the floating point number that represents the significant digits of that number. The exponent is the power to which the number in the mantissa is to be raised.

28 Floating Point Numbers in Binary
Floating point notation allows us to store a much larger range of numbers and store them with much more accuracy. To show your understanding of floating point notation in binary, you will usually be given two binary values (one for the mantissa and one for the exponent) and asked to convert them to denary. E.g. What is the decimal equivalent of the floating point number where is the mantissa and 011 is the exponent?

29 Floating Point Numbers in Binary
What is the decimal equivalent of the floating point number where is the mantissa and 011 is the exponent? Step 1: Convert the exponent to denary, in this example 011 = 3. Step 2: The mantissa started as Move the decimal point 3 places to get Step 3: Write as denary 6.5 ( ) 8 4 2 1 . 1/2 1/4 1/8 1/16

30 Example 2 What is the decimal equivalent of the floating point number ? Exponent = = 4, thus binary point has “floated” four places to the left. Mantissa started as , move 4 place it becomes : Thus = 9.25 in denary. 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32

31 Converting a Denary Number to Floating Point
Consider we were given the number and needed to convert it to floating point binary, using 5 bits for the mantissa and 3 bits for the exponent = in pure binary Move the point 3 places to the left. Mantissa =10001 Exponent = 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32

32 Task Convert the denary numbers into floating point numbers (use a 6 bit mantissa and 3 bit exponent): +2.25 +6.75 +4.5 Convert the following floating point numbers into denary:

33 Normalisation of Floating Point Numbers
The precision of floating point binary depends on the number of bits used to represent the mantissa. The larger number of bits used in the mantissa will allow a number to be represented with greater accuracy, however this will reduce the number of bits in the exponent and consequently the range of values which can be represented. A key point of note is that there will always be a trade-off between accuracy and range when storing real numbers using floating point notation, as there will always be a set number of bits allocated to storing real numbers with the potential to increase or decrease the number of bits used for the mantissa against the number of bits used for the exponent. In calculating complex qualifications sometimes the relationships between exponent vs. mantissa can swap (2nd Year University stuff). (Known as accuracy and range in the Specification) For example, could be × 105 with 5 digits for the mantissa or × 104 with 4 digits for the mantissa. In order to achieve the most accurate representation possible with the number of bits available for a mantissa, the number should be written with no leading 0s. Normalisation is the process of removing these leading 0s.

34 Normalisation of Floating Point Numbers
Mantissa Exponent 100 Step 1: Convert the exponent to denary, 100 = 4. Step 2: Move the decimal point in the mantissa so that it is before the first 1, so becomes The decimal place has moved 3 places. Step 3: Because we’ve just moved the decimal point we need to adjust the exponent to take this into account. As we moved to the right, we subtract the number of moves from our exponent: 4 − 3 = 1. So our new exponent is 1. Step 4: Our new, normalised, floating point number is , which is the same as (work it out if you don’t believe it!). But now we have loads more space in the mantissa so we can store a much more accurate number if we wanted to.

35 Example 2 A binary number is presented in this format, a 5 bit mantissa and 3 bit exponent Q: Is this in normalised form ? Answer: No, consider the first two bits. Exponent = 011 = 3 Mantissa = , which becomes Number of moves right is 1. As we moved right by 1, we subtract this from the exponent to become becomes

36 Example 3 This time we are going to look at if the bit length of the Mantissa and Exponent are provided… E.g. A real binary number may be represented in floating point binary notation using 5 bits for the mantissa and 3 bits for the exponent, both in two’s complement binary. Give the normalised version of the number The answer would be… WHY?

37 Task Give the normalised version of all the following numbers using 6 bits for the mantissa and 3 bits for the exponent.

38 Negative Floating Point Binary Numbers
It is also possible to have a negative exponent and move the decimal point to the left. This is achieved by storing the exponent as a two’s complement binary number. Step 1: Convert the exponent to denary; in this example 110 becomes 010 or −2. Step 2: The mantissa started as Move the decimal point left 2 places to get Step 3: Write as denary ( ). Mantissa Exponent 01110 110 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64

39 Negative Floating Point Binary Numbers
The same method can be used to deal with negative numbers, representing the mantissa using two’s complement. Step 1: Convert the exponent to denary, 010 = 2. Step 2: Convert the mantissa to negative binary by flipping the bits, in this example becomes (think twos complement). Step 3: The mantissa started as Move the decimal point 2 places to get Step 4: Write 0.01 as denary −0.25; don’t forget it’s negative! Mantissa Exponent 11111 010 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64

40 Example 2 Mantissa Exponent 10101 011 Exponent = 011 = 3 Mantissa = = Converted to 2s comp = Move Mantissa 3 places right = -2.5 8 4 2 1 . 1/2 1/4 1/8 1/16 1/32 1/64

41 Adding Floating Point Numbers
You will be pleased to know that addition and subtraction of floating point numbers are very similar operations. This example will walk you through adding the two numbers below: Step 1: In order to add two floating point numbers together, their exponent must be the same. We achieve this by changing the exponent of our second number from (3) to (4). Because we’ve changed the exponent of the second number, we need to adjust its mantissa to take account of this, so the mantissa becomes (as is the same as ). Mantissa (10 bit) Exponent (6 bit) 000100 000011

42 Adding Floating Point Numbers
Step 2: Now we can add together the two mantissas using our usual method: Step 3: So the result is 1

43 Adding Floating Point Numbers
Lets check it back… Our original data looked like this… = = = = = 15.5 Answer = = 15.5 Mantissa (10 bit) Exponent (6 bit) 000100 000011

44 Subtracting Floating Point Numbers
Subtraction is the same as addition except that you convert the number to be subtracted to a negative number using two’s complement. Step 1: Just like addition, the exponents of the two numbers must be the same. We achieve this by changing the exponent of our second number from (2) to (3). Because we’ve changed the exponent of the second number, we need to adjust its mantissa to take account of this, so the mantissa becomes Mantissa (10 bit) Exponent (6 bit) 000011 000010

45 Subtracting Floating Point Numbers
Step 2: Now convert the mantissa of the second number to a negative one, using two’s complement: becomes Step 3: Now we can add together the two mantissas. Step 4: The result is or 3.75 (X remember to ignore any carried values past the magnitude of the starting bit length when using 2’s comp). 1

46 Subtracting Floating Point Numbers
Lets check it back… Our original data looked like this… = = = = – 2.25 = = 3.75 Mantissa (10 bit) Exponent (6 bit) 000011 000010

47 Character Sets As well as representing numbers, binary can also be used to represent characters. For example, might represent the character ‘a’. The different representations of letters and symbols in binary are called character sets. The two most commonly used character sets are called ASCII and UNICODE.

48 Character Sets ASCII encodes 127 different letters and symbols using 7-bit binary codes, which is perfect for writing in English. You won’t need to know the ASCII value of every character but it’s worth taking the time to learn where lower case letters (97) and uppercase letters (65) start. Languages like Japanese, Arabic and Hebrew all have a much larger alphabet than English. To get around this problem, UNICODE was invented. UNICODE uses up to four bytes to represent each character (depending on the version). This means that it can represent up to different characters – more than enough to cope with most of the world’s languages.


Download ppt "A-level Computer Science"

Similar presentations


Ads by Google