1-Data Representation
1-Data Representation
Data Representation
Debugging
• I mentioned programmers use
Hexadecimal to debug code instead of
using Binary.
NONSENSE!
A
P952BR
load ofcould
rubbish,
be aitproduct
means nothing
code, a post/ZIP
because code,
it has
a car registrationnonumber,
meaning.a robot, anything.
This is data.
In your books
• Write down a series of data about
yourself.
• No context or meaning.
• Text, numbers, countries, names, etc.
• Once you’re done, show them to your
partner.
Can you guess what my data means?
Data
32
12
Russia
0
2
Ford Mustang
Final Fantasy 7
Fortnite
What is this?
Data Context Comment
P952BR Product code Still data, what product?
@bbcclick Twitter username Whose address?
359 Price in Pakistani Rupees What’s the price for?
0 1 1 0 0 1 1 0 1
+
0 1 0 0 1 1 1 1 0
Result 1 0 1 1 0 1 0 1 1
0 0 0 0
Signed bit:
Remaining bits
0 = Positive
for storing value
1 = Negative
Signed bit integers
0 0 1 1 =3
1 0 1 1 = -3
Two’s complement
• This can cause issues when it comes to
doing maths in binary.
• So we can use One’s Complement or
Two’s Complement.
• For this course and exam you need to use
Two’s Complement.
• (This is different but very similar to what
we did with addition the other day)
One’s complement is easy
• You just invert the binary
000 = 0
111 = -0
0 0 0
So this is why
we now use
Two’s
1 1 1
Complement
Two’s complement
0 0 0 0
Signed bit:
Flip all remaining bits and add 1 for
0 = Positive
storing value
1 = Negative
Range of storable integers
4 0 through 15 -8 through 7
8 0 through 255 -128 through 127
16 0 through 65,535 -32,768 through 32,767
32 0 through 4.29 billion -2.15 billion through 2.15 billion
Think of it as a second binary language
Denary 4-bit binary
-8 1000
-7 1001
-6 1010
-5 1011
-4 1100
-3 1101
-2 1110
-1 1111
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
The most confusing part about this
• The number of bits you use, affects the
end value.
• In the exam, it will almost always be 8
bit.
• But to give an example…
Bit difference
4 bit 8 bit
Denary 3 Denary 3
Binary 0011 Binary 0000 0011
Complement 1101 Complement 1111 1101
12 bit 16 bit
Denary 3 Denary 3
Binary 0000 0000 0011 Binary 0000 0000 0000 0011
Complement 1111 1111 1101 Complement 1111 1111 1111 1101
Binary
• We know the smallest number we can
represent in binary is 0.
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 =0
-128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
-128 64 32 16 8 4 2 1
0 1 1 1 0 1 0 1
64 32 16 4 1
+117
-128 64 32 16 8 4 2 1
1 0 0 0 1 0 1 1
-128 8 2 1
-117
1
0 0 1 1 =3
(-5 + 2)
Two’s complement
1
1 0 1 1 = -5
1+0 = 1
1+1 = 10
0 0 1 0 =2 1+0 = 1
1+0 = 1
1 1 0 1 = -3
(-2 + -3)
Two’s complement
1
1 1 1 0 = -2
0+1 = 1
1+0 = 1
1 1 0 1 = -3 1+1 = 10
1+1+1 = 11
1 Overflow
1
1 0 1 1 = -5
Write some challenges for your friend
• Give your friend 10 questions or more.
• Adding two 8 bit two’s complement
numbers together.
• Mix up your questions! Give binary give
denary, make them work!
• One positive and one negative number.
• Two positive numbers together.
• Two negative number together.
Text to binary
• We’ve done this before, but lets review.
• We use a type of binary called…
• ASCII
• Which stands for?
• American Standard Code for Information
Interchange
Unicode
• This is similar to HEX, but whereas Hex
can hold a value of 16 characters.
• Unicode can hold around 65000
• Hello in Unicode:
• \u0048\u0065\u006c\u006c\u006f
• 你好 in Unicode:
• \u4f60\u597d
Solve Practical Activity 1.2
Page 21 128 64 32 16 8 4 2 1
Try writing messages again using this table
128 64 32 16 8 4 2 1
Worksheet 1.3
• Answers:
1. hello computing is fun
Worksheet 1.3
• 1 If each character can Binary Octal Decimal Hexadecimal
0000 0 0 0
have only 4 bits, how many 0001
0010
1
2
1
2
1
2
16 0111
1000
7
10
7
8
7
8
1001 11 9 9
1010 12 10 A
1011 13 11 B
1100 14 12 C
1101 15 13 D
1110 16 14 E
1111 17 15 F
Worksheet 1.3
• 2 If each character can have 8 bits, how many different
characters can you have?
256
• 3 Create a set of codes that use 8 bits for capital letters and
numbers.
You cannot use the same code twice, and you cannot use any of
the numbers used in the code at
the start of this worksheet.
E.g. Use 30-56 for capitals (e.g. A = a +20) and 100 to 109 for
Converting images to binary
• Images are made up of pixels. What are they?
• Tiny squares, which can be given a colour value, and
when combined make a picture.
• Pixel means:
PICture
ELement
Binary
• We can also use binary to create images.
Obviously that’s just black and white
• We can add more colours, but what issue could that cause?
• More memory usage.
• If we’re storing 1 bit of data per pixel for a 256 pixel image.
That is: (1x256)/8 = 32Bytes
• If we add 8 bits of colour data per pixel, that same 256
image becomes: (8x256)/8 = 256Bytes
• If we add 64 bits of colour data per pixel, that same 256
image becomes: (64x256)/8 = 2048Bytes (2KB)
The amount of colour available
scales exponentially
Easier to see why with grayscale
We often use Hex for colours
Basic colours
Color HTML / CSS Name Hex Code#RRGGBB Decimal Code(R,G,B)
Megabyte 1024 kilobytes MP3 files take up about 1 megabyte per minute
Gigabyte 1024 megabytes 1 gigabyte can hold about 300 MP3 songs
Terabyte 1024 gigabytes 1 terabyte can hold about 100 DVD-quality films