Data Types:
Although data is always stored in binary by computes, 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.
What are the data types?
Integer:
An integer is a whole number. Integers include 0 and negative numbers, they just can’t have a
fractional part. Integers are useful for counting things. Examples of integers are:
6, 47238, -12, 0, 15
Real:
Real numbers are positive or negative numbers which can, but not necessarily, have a fractional
part. Reals are useful for measuring things. All integers ae real numbers. Real numbers can also be
represented using floating point. Examples of reals are:
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 £. Examples of characters are:
R, {, 7, “,
String:
A string is a collection of characters. While a string can be used to store a single character, they also
can 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. Examples of
strings are:
Hello, world! 07789
Boolean:
Boolean is named after George Bool. Values taken by the Boolean data type are restricted to True or
False. Booleans are useful for recording data that can only take 2 values, like the state of a power
button, or whether a line of code has been executed. Example of Boolean data:
TRUE, FALSE
Representing Positive Integers in Binary:
Computers can store whole numbers using binary. Computers count in base 2, where each step-in
place represents a value of 2 times the previous place.
A single binary digit is called a bit, and 8 binary digits can be combined to form a byte. 4 binary digits
can be combined to form a nybble.
Binary to Decimal:
The least significant bit of a binary number is 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, then 8, etc.
, 8 (23) 4 (22) 2 (21) 1 (20)
1 1 0 1
The table shows the place value of each digit as well as the digits value (0 or 1). To work out what
the number is, multiply the digit to its place value and add to the total. For the diagram above, we
have (8×1) + (4×1) + (2×0) + (1×1) = 13, so the binary 1101 is 13 in decimal.
Decimal to Binary:
If you have a decimal (denary) number to convert it into binary, the first step is to find the largest
power of 2 which is smaller than the number your converting, then write out the place values in
powers of 2 up to this power.
32 16 8 4 2 1
Now we need to place a 1 or 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 you subtract the place value from the value and use the
result for the next stage. For example:
The most significant has a value of 32, which is less than 47. Therefore, we write a 1 under the 32,
then subtract 32 from 47 giving us a value of 15.
32 16 8 4 2 1
1
We now look at the most significant bit and follows the same steps as before. This time, the bit
represents 16 and our value is 15. The bit value is greater than our value, so we place a 0.
32 16 8 4 2 1
1 0
Next you look at the most significant bit, which is 8. 8 is 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
The next most significant bit is 4. 4 is smaller than 7, so we place a 1. Our new value is 3.
32 16 8 4 2 1
1 0 1 1
Our next most significant bit is 2. 2 is smaller than 3, so we place a 1 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 the binary for 47, which is 101111. You can check your conversion by converting from
binary to decimal. It’s not unusual to see binary numbers represented as a whole number of bytes
(multiple of 8 bits) by adding leading 0s. this doesn’t affect the value of the number. To be
represented as a byte, 47 would be written as 00101111.
Binary Addition:
When adding binary, there are 4 simple rules to remember:
0+0=0
, 0+1=1
1 + 1 = 10
1 + 1 + 1 = 11
For example, add the binary numbers 1011 and 1110.
First, you place the 2 binary numbers above one another, so that the digits line up.
1 0 1 1
1 1 1 0
Starting with the least significant bits, add the values in each column and place the total below. For
the first column, the second rule applies.
1 0 1 1
1 1 1 0
1
Move to the next column. This time rule 3 applies. In the case that the result of addition for a single
column is more than one digit, place the first digit of the result in small writing under the next most
significant bit’s column.
1 0 1 1
1 1 1 0
1 1
1
On the next column 0 and a 1. This uses rule 3 applies again. Therefore, the result is 10. Because 10
is 2 digits long, the 1 is written small writing under the next most significant bit’s column.
1 0 1 1
1 1 1 0
0 1 1
1 1
Moving to the final column, where there are 3 1s. rule 4 applies, so the result is written under the
next most significant bits column, but it can be written in full as there are no more columns to add.
1 0 1 1
1 1 1 0
1 1 0 1 1
1 1
The result is read off in full size numbers at the bottom of each column. In this case 1011 + 1110 =
11001.
Negative numbers in binary:
Binary can be used to represent negative numbers. There are multiple 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 – signs in front of a number. However, binary can only use 0s
and 1s, so we have to somehow represent + and – using 0 and 1.
Although data is always stored in binary by computes, 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.
What are the data types?
Integer:
An integer is a whole number. Integers include 0 and negative numbers, they just can’t have a
fractional part. Integers are useful for counting things. Examples of integers are:
6, 47238, -12, 0, 15
Real:
Real numbers are positive or negative numbers which can, but not necessarily, have a fractional
part. Reals are useful for measuring things. All integers ae real numbers. Real numbers can also be
represented using floating point. Examples of reals are:
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 £. Examples of characters are:
R, {, 7, “,
String:
A string is a collection of characters. While a string can be used to store a single character, they also
can 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. Examples of
strings are:
Hello, world! 07789
Boolean:
Boolean is named after George Bool. Values taken by the Boolean data type are restricted to True or
False. Booleans are useful for recording data that can only take 2 values, like the state of a power
button, or whether a line of code has been executed. Example of Boolean data:
TRUE, FALSE
Representing Positive Integers in Binary:
Computers can store whole numbers using binary. Computers count in base 2, where each step-in
place represents a value of 2 times the previous place.
A single binary digit is called a bit, and 8 binary digits can be combined to form a byte. 4 binary digits
can be combined to form a nybble.
Binary to Decimal:
The least significant bit of a binary number is 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, then 8, etc.
, 8 (23) 4 (22) 2 (21) 1 (20)
1 1 0 1
The table shows the place value of each digit as well as the digits value (0 or 1). To work out what
the number is, multiply the digit to its place value and add to the total. For the diagram above, we
have (8×1) + (4×1) + (2×0) + (1×1) = 13, so the binary 1101 is 13 in decimal.
Decimal to Binary:
If you have a decimal (denary) number to convert it into binary, the first step is to find the largest
power of 2 which is smaller than the number your converting, then write out the place values in
powers of 2 up to this power.
32 16 8 4 2 1
Now we need to place a 1 or 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 you subtract the place value from the value and use the
result for the next stage. For example:
The most significant has a value of 32, which is less than 47. Therefore, we write a 1 under the 32,
then subtract 32 from 47 giving us a value of 15.
32 16 8 4 2 1
1
We now look at the most significant bit and follows the same steps as before. This time, the bit
represents 16 and our value is 15. The bit value is greater than our value, so we place a 0.
32 16 8 4 2 1
1 0
Next you look at the most significant bit, which is 8. 8 is 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
The next most significant bit is 4. 4 is smaller than 7, so we place a 1. Our new value is 3.
32 16 8 4 2 1
1 0 1 1
Our next most significant bit is 2. 2 is smaller than 3, so we place a 1 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 the binary for 47, which is 101111. You can check your conversion by converting from
binary to decimal. It’s not unusual to see binary numbers represented as a whole number of bytes
(multiple of 8 bits) by adding leading 0s. this doesn’t affect the value of the number. To be
represented as a byte, 47 would be written as 00101111.
Binary Addition:
When adding binary, there are 4 simple rules to remember:
0+0=0
, 0+1=1
1 + 1 = 10
1 + 1 + 1 = 11
For example, add the binary numbers 1011 and 1110.
First, you place the 2 binary numbers above one another, so that the digits line up.
1 0 1 1
1 1 1 0
Starting with the least significant bits, add the values in each column and place the total below. For
the first column, the second rule applies.
1 0 1 1
1 1 1 0
1
Move to the next column. This time rule 3 applies. In the case that the result of addition for a single
column is more than one digit, place the first digit of the result in small writing under the next most
significant bit’s column.
1 0 1 1
1 1 1 0
1 1
1
On the next column 0 and a 1. This uses rule 3 applies again. Therefore, the result is 10. Because 10
is 2 digits long, the 1 is written small writing under the next most significant bit’s column.
1 0 1 1
1 1 1 0
0 1 1
1 1
Moving to the final column, where there are 3 1s. rule 4 applies, so the result is written under the
next most significant bits column, but it can be written in full as there are no more columns to add.
1 0 1 1
1 1 1 0
1 1 0 1 1
1 1
The result is read off in full size numbers at the bottom of each column. In this case 1011 + 1110 =
11001.
Negative numbers in binary:
Binary can be used to represent negative numbers. There are multiple 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 – signs in front of a number. However, binary can only use 0s
and 1s, so we have to somehow represent + and – using 0 and 1.