0% found this document useful (0 votes)
6 views15 pages

1.4.1. Data Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

1.4.1. Data Types

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

OCR Computer Science AS Level

1.4.1 Data Types


Intermediate Notes

www.pmt.education
Specification:

1.4.1 a)
● Primitive data types
○ Integer
○ Real / floating point
○ Character
○ String
○ Boolean
1.4.1 b)
● Represent positive integers in binary
1.4.1 c)
● Negative numbers in binary
○ Sign magnitude
○ Two’s complement
1.4.1 d)
● Addition and subtraction of binary integers
1.4.1 e)
● Represent positive integers in hexadecimal
1.4.1 f)
● Convert positive integers between binary, hexadecimal and
denary
1.4.1 g)
● Representation and normalisation of floating point numbers in
binary
1.4.1 h)
● How character sets are used to represent text
○ ASCII
○ Unicode

www.pmt.education
Data Types

The way in which data is represented ​varies ​between different types of data. When writing
a program, it’s essential to make sure data is being stored with the right​ data type​, so that
the right ​operations ​can be performed on it.

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
Real numbers are positive or negative numbers which can, but do not necessarily, have a
fractional part​. Reals are useful for measuring things.

0 -71.5 5.01 -80.8 15


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 Σ ほ
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.

Hello, world! 54jR

www.pmt.education
Boolean
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

Representing Positive Integers in Binary

As explained earlier, integers are whole numbers. Computers can store whole numbers
using binary. Just like humans count in base 10, computers count in base 2, where each
step in place represents a ​value of two times ​the previous place.

A single ​b​inary dig​it​ is called a ​bit​, and eight binary digits can be combined to form a ​byte​.

Binary to Decimal
The​ least significant bit ​of a binary number is the one furthest to the right, while the​ most
significant bit​ is furthest to the left. When representing positive integers, the least
significant bit always represents a value of 1, with the 2nd least significant bit representing
a value of 2, then 4, 8 etc.

8 (2​)
3​
4 (2​)
2​
2 (2​)
1​
1 (2​)
0​

1 1 0 1
The diagram above shows the place value of each digit, as well as the digit’s value (either
a ​0​or a ​1​
). To work out what the number is, ​multiply ​the digit by its place value and add to
a total.

For the diagram above, we have ​(8​


×1)+(4×1)+(2×0)+(1×1) = 13​so the binary
1101​is ​13​in decimal.

www.pmt.education
Decimal to Binary
If you have a decimal (denary) number to convert into binary, the first step is to find the
largest power of two​ which is ​smaller ​than the number you’re converting. Then write out
place values in powers of two up to this power.

For example, if we were converting the decimal 47 into binary, we would write out place
values up to 32. 64 is the next power of two and it’s greater than 47 so we don’t write it.

32 ​
(2​)
5​
16 ​
(2​)
4​
8 (
​2​)
3​
4 (
​2​)
2​
2 (
​2​)
1​
1 (
​2​)
0​

Now we need to place a 1 or a ​0​in each position so that the total adds up to 47. Starting
from the most significant bit (left hand side) we write a 1 if the place value is​ less than or
equal to​ our value and a ​0​​otherwise​. If we write a 1, then we subtract the place value from
our value and use the result for the next stage.

For example: the most significant bit has a value of 32, which is less than 47. Therefore,
we write a 1 under 32 and subtract 32 from 47 giving us a new value of 15.

32 16 8 4 2 1

1
We now look at the next most significant bit, and follow the same steps as before. This
time the bit represents 16 and our value is 15. The bit value is greater than our value and
so we place a ​0​.

32 16 8 4 2 1

1 0
Next up is 8, smaller than 15. Therefore we place a 1 and our new value is ​15-8 = 7​
.

32 16 8 4 2 1

1 0 1

www.pmt.education
The next most significant bit is 4. 4 is smaller than 7 and so we place a 1. Our new value is
7-4 = 3​ .

32 16 8 4 2 1

1 0 1 1
Next up is 2, again smaller than our value of 3. We place a one and our new value is 1.

32 16 8 4 2 1

1 0 1 1 1
Finally, the bit represents 1 and our value is 1, so we place a 1.

32 16 8 4 2 1

1 0 1 1 1 1
We now have that the binary ​101111​represents the decimal number 47. You can check
your conversion using the method explained earlier for converting from binary to decimal.

It’s not unusual to see binary numbers represented as a ​whole number of bytes​ (a multiple
of eight bits) by adding leading zeros. This does not affect the value of the number. To be
represented as a byte, 47 would be written as ​00101111​ , with two leading ​0​
s.

Binary Addition
When adding binary, there are four simple rules to remember:

1. 0 +​0 + 0 = 0
2. 0 + ​
0 + 1 = 1
3. 0 + ​
1 + 1 = 10
4. 1 + 1 + 1 = 11

Remember that ​10​in binary is 2 in decimal and ​11​is three.

www.pmt.education
Example:​ Add the binary numbers ​1011​and ​1110​
.

Place the two binary numbers above each other so that the
1 0 1 1
digits line up​.
+ 1 1 1 0

Starting from the least significant bits (the right hand side),
1 0 1 1
add the values in each column​ and place the total below.
+ 1 1 1 0 For the first column (highlighted), rule 2 from above
applies.
1

1 0 1 1 Move on to the next column. This time rule 3 applies. In the


case that the result of addition for a single column is ​more
+ 1 1 1 0 than one digit​, place the first digit of the result in ​small
1
0 1 writing​ under the ​next most significant​ bit’s column.

1 0 1 1 On to the next column, where there is a ​0​ , a 1 and a small


1. In this case, rule 3 applies again. Therefore the result is
+ 1 1 1 0
10​. Because ​10​is​ two digits long​, the 1 is written in small
1
0​
1
0 1 writing under the next most significant bit’s column.

Moving on to the most significant column where there are


1 0 1 1
three 1s. Rule 4 applies, so the result for this column is ​11​
.
+ 1 1 1 0 The first digit of the result is written under the next most
significant bit’s column, but it can be written full size as
1 1​
1
0​
1
0 1
there are no more columns to add.

Finally, the result is ​read off from the full size numbers​ at
1 1 0 0 1 the bottom of each column. In this case, ​1011 + 1110 =
11001​ .

After carrying out binary addition, it’s a good idea to ​check your answer ​by converting to
decimal if you have time.

www.pmt.education
Negative Numbers in Binary

So far we’ve covered conversion between decimal and binary for positive integers.
However, binary can represent ​negative numbers​ using a few different methods. These
methods set out rules for how a bit string should be treated, giving a ​special meaning​ to
certain bits which allows for the representation of negative numbers.

Sign Magnitude
The most basic way to represent negative numbers in binary is called sign magnitude
representation. This is the ​equivalent of adding a + or - sign​ in front of a number. However,
binary can only use ​0​
s and ​1​s, so we have to somehow represent + and - using ​0​and ​1​ .

The convention used is that a​ leading ​1​is added for a negative number, and a​ leading ​0​is
added for a positive number.

For example, the binary ​1101​represents the decimal number ​13​ . Converting to sign
magnitude means adding a ​0​ , or to represent ​-13​
, add a leading ​1​
.

Binary Sign Magnitude Sign Magnitude


13 +13 -13

1101 0​
1101 1​
1101

Converting from sign magnitude to decimal is as simple as making a note of the most
significant bit, remembering the sign and discarding the leading bit. Then convert the
remaining bits to decimal using the method explained earlier and add the sign.

For example, the sign magnitude number ​10110​is negative, because it starts with a ​1.​
Remove the ​1​and we’re left with ​0110​which is ​6​​(4+2)​in decimal. Add on the minus
sign and we have our result: ​-6​
.

Two’s Complement
Another method of representing negative numbers in binary, ​two’s complement ​has the
added advantage of making binary arithmetic with negative numbers ​much more simple​.

Two’s complement works by making the most significant bit ​negative​. For example, with
four bits (half a byte) the most significant bit, usually 8, represents -8.

Converting to two’s complement is as simple as flipping all of the bits in the positive
version of a binary number and adding one. For example, the binary half-byte representing
7 is ​0111​
. Flip all the bits and you get ​1000​
, adding one gives us ​1001​
.

www.pmt.education
Writing in the bit values, we can see how this works.

-8 4 2 1

1 0 0 1
Calculating the equivalent in decimal, using the method explained earlier, we have:

(-8×1)+(4×0)+(2×0)+(1×1) = -
​7

Subtracting in Binary using Two’s Complement

Two’s complement ​makes subtraction in binary easy​. Subtracting a number from another
is the same as adding a negative number. This is how binary subtraction works.

Example:​ Subtract 12 from 8.


In five bit two’s complement, 8 is ​01000​and -12 is
10100​ . ​Five is the minimum​ number of bits
required in order to represent ​-12​ .
-16 8 4 2 1

0 1 0 0 0 The two’s complement numbers are then ​added


using the same technique for adding that was
+ 1 0 1 0 0 explained earlier before the result can be read off
as ​11100​.
1 1 1 0 0
Checking the result, ​-16 + 8 + 4 = -4​so the
calculation is correct.

www.pmt.education
Hexadecimal

In the same way that decimal is base 10, and binary is base 2, ​hexadecimal is base 16​. In
addition to the numbers ​0-9​
, hexadecimal makes use of the characters ​A-F​to represent
10-15​ .

Decimal

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0 1 2 3 4 5 6 7 8 9 A B C D E F
Hexadecimal

Just like binary, place values in hexadecimal start with 1 (16​0​) and go up in powers of 16.

For example, the hexadecimal number 2​D3 = 723

256 (16​)
2​
16 (16​)
1​
1 (16​)
0​

2 D 3
Remembering that E represents 14 in decimal and that F represents 15, we have:

(2×256)+(13×16)+(3×1) = 723

www.pmt.education
Converting from hexadecimal to binary
To convert hexadecimal to binary, first convert each hexadecimal digit to a ​decimal digit
and then to a ​binary nybble​ (four bits, half a byte) before ​combining the nybbles ​to form a
single binary number.

B2
Split into hexadecimal digits

B 2
Convert hexadecimal to decimal

11 2
Convert decimal to binary nybbles

1011 0010
Combine binary nybbles

10110010

128 64 32 16 8 4 2 1

1 0 1 1 0 0 1 0
128 + 32 + 16 + 2 = 178

B2​= ​178

www.pmt.education
Converting from hexadecimal to decimal
One way to convert from hexadecimal to decimal is to​ first convert to binary​, as explained
above, and then convert ​from binary to decimal​. Alternatively, use the place values of
hexadecimal to convert directly to decimal.

256 (16​)
2​
16 (16​)
1​
1 (16​)
0​

4 C 3
For example, 4C3 in hexadecimal is ​(4​
×256​
)+(12​
×16​
)+(3​
×1​
) = 1219

Floating Point Numbers in Binary

You can think of floating point binary as being like ​scientific notation​. Take the following
example:

Floating point numbers can be split into two parts: ​mantissa ​and ​exponent​. In this case,
the ​mantissa ​is 6.67 and the ​exponent ​is -11. When combined, the mantissa and exponent
provide all the information needed to work out the actual value being represented.

In this case, the scientific notation represents the value 0.0000000000667. The value 6.67
is shifted 11 times from the decimal point.

We can do the same in binary, provided that we include information about the size of the
mantissa and exponent. We also dedicate a single bit to the ​sign ​- whether a number is
positive or negative.

Take for example a structure with a leading sign bit, an 8-bit mantissa and a 4-bit two’s
complement exponent.

S M E

0 1 1 0 0 1 0 0 1 0 1 0 1

Just as before, a sign of ​0​represents a ​positive number​ and a ​1​represents a ​negative


number​.

The mantissa is always taken to have the binary point (the equivalent of a decimal point,
but in binary) after the most significant bit. So this mantissa is actually ​1.1001001​
.

www.pmt.education
Next we ​convert the exponent to decimal ​using the method explained earlier. In this case,
the exponent is 5.

Combining the three parts, we need to move the binary point​ five places to the right​, giving
us 110010.01. We can then convert this to decimal as follows:

0.5 0.25
32 16 8 4 2 1
(2​ )
-1​
(2​ )
-2​

1 1 0 0 1 0 • 0 1

32 + 16 + 2 + 0.25 = ​
50.25

Second example
As before, we’re using a format with a single sign bit, an 8-bit mantissa and a 4-bit two’s
complement exponent.

S M E

1 0 1 0 1 1 0 0 0 1 1 0 1

The sign is 1 and so we’re dealing with a negative number. The mantissa is 0.101101000
and the exponent is (remembering it’s in two’s complement) -8 + 4 + 1 = -3.

We move the binary point from between the two most significant bits of the mantissa three
places to the left, giving us 0.0001011.

1 1
2
1
4
1
8
1
16
1
32
1
64
1
128

0 • 0 0 0 1 0 1 1

11
Which is 128
in base 10.

www.pmt.education
Normalisation

Floating point numbers are ​normalised ​in order to ​provide the maximum level of precision
for a given number of bits​. Normalisation involves ensuring that floating point numbers
start with​ ​01​(for a positive number) or ​10​(for negative numbers).

Example:​ Normalise the binary number ​00100101 w


​ hich is a floating point number with a
four bit mantissa ​and a ​four bit exponent​.

First, split the number into mantissa and exponent.

0 0 1 0 0 1 0 1
Mantissa Exponent

Next, ​adjust the mantissa​ so that it starts ​01​or ​10​


. In this case, because we’re dealing
with a positive number, we will move all of the bits ​one place to the left​ and ​add a zero to
the end ​of the mantissa. Our new mantissa is ​0100​ .

Because we’ve made the mantissa ​bigger ​by shifting the bits one position to the left, we
must ​reduce the exponent ​by one so as to ​ensure the same number is still represented​.
The current exponent is 5​10​ so, ​subtracting one​, the new exponent must be 4​10​ which is
0100​2​ in binary.

0 1 0 0 0 1 0 0
Mantissa Exponent

We now have a mantissa that starts with the digits ​01​


. A ​positive normalised ​number.

www.pmt.education
Character Sets for Representing Text

A character set is a published collection of codes and corresponding characters which can
be used by computers for representing text.

Two widely used character sets are ​ASCII ​and ​Unicode​.

ASCII
Standing for American Standard Code for Information Interchange, ASCII was the leading
character set before Unicode. ASCII uses 7 bits to represent 2​7​ = 128 different characters.
The capital letters A-Z are represented by codes 65-90 while the lower case letters a-z
correspond to codes 97-122. There are also codes for ​numbers ​and ​symbols​.

While 128 characters is plenty for standard letters, numbers and symbols, ASCII soon
came into trouble when computers needed to represent other languages with different
characters.

Unicode
Unicode solves the problem of ASCII’s limited character set. Unicode uses a ​varying
number of bits​ allowing for over 1 million different characters, many of which have yet to
be allocated. Because of this, Unicode has enough capacity to represent a wealth of
different languages, symbols and emoji.

www.pmt.education

You might also like