0% found this document useful (0 votes)
17 views

1 Number System

Here are the key steps for two's complement subtraction in binary: 1. Write the numbers in binary as positive values. 2. Flip all the bits (1s become 0s and 0s become 1s) of the number to be subtracted. 3. Add 1 to the result. 4. Perform binary addition by adding the numbers based on the standard binary addition rules. This allows binary to represent negative numbers and perform subtraction by treating it as addition of the two's complement.

Uploaded by

fangkevin33
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

1 Number System

Here are the key steps for two's complement subtraction in binary: 1. Write the numbers in binary as positive values. 2. Flip all the bits (1s become 0s and 0s become 1s) of the number to be subtracted. 3. Add 1 to the result. 4. Perform binary addition by adding the numbers based on the standard binary addition rules. This allows binary to represent negative numbers and perform subtraction by treating it as addition of the two's complement.

Uploaded by

fangkevin33
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 137

1.

Data Representation
AS Computer Science
1.1 Data Representation
1.2 Multimedia
1.3 File Compression
1.1 Data Representation
1.1 Data Representation

1.1.1 Number System

1.1.2 Binary Number System

1.1.3 Hexadecimal Number System

1.1.4 Binary-coded decimal(BCD) system

1.1.5 ASCII code and Unicode


 https://www.youtube.com/watch?v=M41M9ATm49M
Number System
Denary 十进制
• The denary number system (decimal)
• base-10 numbering system
• 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9

• used to represent any quantity,


• including whole numbers, fractions, and decimals
• Each digit in a denary number represents a power of 10
• What’s the value of 3268 in denary?

• 3268 = (3x1000)+(2x100)+(6x10)+(8x1)
3268 = (3x103)+(2x102)+(6x101)+(8x100)
3268 = (3x103)+(2x102)+(6x101)+(8x100)
Analogue data 模拟数据
 As humans, we process data that is called analogue data.
 Everything we see, hear, feel and smell is a continuous 连续的 stream of
analogue data to our senses.

 Three examples of analogue data?


Digital data 电子数据
The components that process data in a computer are made up of
very small logic gates 逻辑门 .
These gates cannot process analogue data, they can only process
digital data. This data is stored in registers 寄存器 .

Any form of data needs to be converted to binary to be processed by a


computer.
Binary 二进制
base-2 numbering system
2 digits: 0(off), 1(on)

The 0’s and 1’s are the binary digits


“Bits” 比特 is short for binary digits
Binary Number System

Binary representation, binary addition, binary shift, One’s compliment, Two’s complement
Binary
• Each digit in a binary number represents a power 幂 of 2,

Value 8 4 2 1
Heading 23 22 21 20

What is the value of 1100 in binary?


1100 in binary is number 12

(1x23)+(1x22)+(0x21)+(0x20) = 1x8 + 1x4 = 12


Let’s work out the 8 bit binary representation for numbers 0 -
15
0 0000 0000 8 0000 1000
1 0000 0001 9 0000 1001
2 0000 0010 10 0000 1010
3 0000 0011 11 0000 1011
4 0000 0100 12 0000 1100
5 0000 0101 13 0000 1101
6 0000 0110 14 0000 1110
7 0000 0111 15 0000 1111
Binary -> Denary
 Denary value = The sum of the products of each digit and it’s
corresponding value

Please convert 01100110 (Binary) to denary representation


Please convert 01100110 (Binary) to denary
representation
Step 1: Find represented value of each digit

128 64 32 16 8 4 2 1
27 26 25 24 23 22 21 20
Please convert 01100110 (Binary) to denary
representation
Step 2: Sum of the products of digits and value

128 64 32 16 8 4 2 1
27 26 25 24 23 22 21 20
0 1 1 0 0 1 1 0

ANS = 1x64+1x32+1x4+1x2 = 102


Denary -> Binary
Two ways, subtraction 减法 or modulu 摸除 / 取余数
Please convert 171 (Denary) to binary representation
Subtraction
Step 1: write down 8 bits and it’s corresponding denary value

128 64 32 16 8 4 2 1
Denary -> Binary
 Step 2:
 Going from left to right, if the value is less than
current number, subtract the value from the number.
Mark that digit as 1.

 Check if 171 > 128?

128 64 32 16 8 4 2 1
1
Denary -> Binary
 Step 3: Repeating until the end

 171-128 = 43
 Check if 43 > 64?

128 64 32 16 8 4 2 1
1 0
Denary -> Binary
 Step 3: Repeating until the end

 171-128 = 43
 Check 43 > 64?
 Check 43 > 32?

128 64 32 16 8 4 2 1
1 0 1
Denary -> Binary
 Step 3: Repeating until the end

 43-32 = 11
 Next valid digit is ‘8’

128 64 32 16 8 4 2 1
1 0 1 0 1
Denary -> Binary
 Step 3: Repeating until the end

 11-8 = 3
 Next valid digit is ‘2’

128 64 32 16 8 4 2 1
1 0 1 0 1 0 1
Denary -> Binary
 Step 3: Repeating until the end

3 – 2 = 1
 Next valid digit is ‘1’
 DONE!!

128 64 32 16 8 4 2 1
1 0 1 0 1 0 1 1
 Subtraction Summary :
 Step 1: write down 8 bits and it’s corresponding denary value
 Step 2: Going from left to right, if the value is less than current
number, subtract the value from the number. Mark that digit as 1.
 Step 3: Repeating until the end
What is 5 in binary? Modulus method

Divisor Dividend & Reminder


quotient 商 余数
5
2

Step 1: perform division by 2 on the value


What is 5 in binary?

Divisor Dividend & Reminder


quotient 商 余数
5
2 2 1

Step 1: perform division by 2 on the value


What is 5 in binary?

Divisor Dividend & Reminder


quotient 商 余数
5
2 2 1
2

Step 2: perform division by 2 on the quotient


What is 5 in binary?

Divisor Dividend & Reminder


quotient 商 余数
5
2 2 1
2 1 0

Step 2: perform division by 2 on the quotient


What is 5 in binary?

Divisor Dividend & Reminder


quotient 商 余数
5
2 2 1
2 1 0
2
Step 3: repeat until quotient becomes 0
What is 5 in binary?

Divisor Dividend & Reminder


quotient 商 余数
5
2 2 1
2 1 0
2 0 1
Step 3: repeat until quotient becomes 0
What is 5 in binary?

Divisor Dividend & Reminder


quotient 商 余数
5
2 2 1
2 1 0
2 0 1
Step 4: read the remainders backwards ANS= 101
Modulus Summary :
Step 1: perform division by 2 on the value
Step 2: perform division by 2 on the quotient
Step 3: repeat until quotient becomes 0
Step 4: read the remainders backwards
9608_s21_qp_11 3c
9608_s21_qp_11 3c

0011 1011
What is 39 in binary?
Divisor Dividend & Reminder
quotient
39
2
What is 39 in binary?
Divisor Dividend & Reminder
quotient
39
2 19 1
ANS = 100111
2 9 1
2 4 1
2 2 0
2 1 0
2 0 1
Binary Addition
there are four rules that you will need to use:

Rule 1 is 0 + 0 = 0
Rule 2 is 1 + 0 = 1
Rule 3 is 1 + 1 = 10 (this is because 10 in binary is 2)
Rule 4 is 1 + 1 + 1 = 11 (this is because 11 in binary is
3)
Binary Addition
Please perform a binary addition for 0110 and 0001.
Binary Addition
Please perform a binary addition for 0110 and 0011.

0 1 1 0
0 0 1 1
+
Binary Addition
Please perform a binary addition for 0110 and 0011.

0 1 1 0
0 0 1 1
+
1
Binary Addition
Please perform a binary addition for 0110 and 0011.

0 1 1 0
0 0 1 1
+ 1
0 1
Binary Addition
Please perform a binary addition for 0110 and 0011.

0 1 1 0
0 0 1 1
+ 1
0 0 1
Binary Addition
Please perform a binary addition for 0110 and 0011.

0 1 1 0
0 0 1 1
+ 1 1
1 0 0 1

In denary, 6 + 3 = 9
Binary Addition
Please perform a binary addition for
1101 1010 and 1001 0110 for 8 bits integer.
Binary Addition
a binary addition for 1101 1010 and 1001 0110.

1 1 0 1 1 0 1 0
1 0 0 1 0 1 1 0
+ 1 1 1 1
1 0 1 1 1 0 0 0 0
Binary Addition
a binary addition for 1101 1010 and 1001 0110.

1 1 0 1 1 0 1 0
1 0 0 1 0 1 1 0
+ 1 1 1 1
1 0 1 1 1 0 0 0 0

Oops, the result is 9 bits long!


Binary Addition – Overflow 溢出
Overflow: If a number larger than the predefined 预设 limits is
generated, an overflow error is created.
Binary Addition - Overflow
If overflow happens,
overflow bits are ignored
We would get a wrong value of our calculation.
9618_s21_qp_13 1c
Binary Subtraction

 To do subtraction in binary, we have to figure out a way to represent


negative number in binary
 - One’s Complement
 - Two’s Complement
Binary Subtraction

 One’s complement:
 Each digit in the binary number is inverted (0 to 1 and 1 to 0).
 Ex:
 0 1 0 1 1 0 1 0 (90)
 1 0 1 0 0 1 0 1 (−90).
Binary Subtraction

 Two’s complement: Note: Two’s complement can also be


used to convert negative value to
Write the number in positive value, same steps
positive form (8-bits)

Flip all the bits, 1 to 0


and 0 to 1

Adding 1 to the flipped


number, ignore any overflow
Please write -35 using Two’s complement
35: 0010 0011
Flip: 1101 1100
Add 1: 1101 1101

ANS = 1101 1101


Two’s complement
Two’s Complement

Normal representation Two’s complement


8 4 2 1 -8 4 2 1

What’s 1000 ‘s value is these two representation?


Two’s Complement

Normal representation Two’s complement


8 4 2 1 -8 4 2 1
1 0 0 0 1 0 0 0

8 -8
Two’s Compliment

Normal representation Two’s complement


8 4 2 1 -8 4 2 1
1 1 1 1 1 1 1 1
ANS = 8 + 4 + 2 + 1= 15 ANS = -8 + 4 + 2 + 1= -1
Two’s Compliment

8 bit’s corresponding value:

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

How do I represent -127?


 It’s very important to use the same size consistent with the question.
 -10 = 0110
 -10 = 1111 0110
Binary Subtraction
Carry out the subtraction 95 – 68 in binary.

Step 1 Convert the two numbers into binary:


95 = 0 1 0 1 1 1 1 1
68 = 0 1 0 0 0 1 0 0
Binary Subtraction
Carry out the subtraction 95 – 68 in binary.

Step 2 Find the two’s complement of 68:


Invert the digits: 10111011
add 1: 1
which gives: 1 0 1 1 1 1 0 0 = −68
Binary Subtraction
Carry out the subtraction 95 – 68 in binary.
Step 3 Add 95 and −68:
-128 64 32 16 8 4 2 1
95 0 1 0 1 1 1 1 1
-68 1 0 1 1 1 1 0 0
+ 1 1 1 1 1 1
27 1 0 0 0 1 1 0 1 1
9618_s21_qp_13 1c
Binary Shift
• Please observe 12, 6, 3 in 4-bits binary representation.
 What have you noticed?
Binary Shift
• Please observe 12, 6, 3 in 4-bits binary representation.
 What have you noticed?

1100 0110 0011


12 6 3
Binary Shift
• Please observe 12, 6, 3 in 4-bits binary representation.
 What have you noticed?

1100 0110 0011


12 6 3
 The ‘1’s are shifted right one position each time
 And the value is halved
Binary Shift 二进制移位
Logical left shift: shift all digits to the left
Logical right shift: shift all digits to the right

Now please perform a logical right shift of one


position on 0011.
Binary Shift

0 0 1 1

0 0 1 1
?
Binary Shift

0 0 1 1

0 0 1 1
?

Ans 0 0 0 1
Binary Shift

 bits that has no place to go are removed


 and the empty slots will be filled with zero
Binary Shift
A binary shift will change the data value,
making it bigger or smaller.

 A shift to the left === multiply a power of 2

 A shift to the right === divide by a power of 2, and only retained the
whole number (3/2 = 1)
Binary Shift

 5 == 0000 0101
 Shift one place to the right (/2)
 0000 0010 == 2
 Shift two place to the right (/4)
 0000 0001 == 1
 Shift one place to the left (*2)
 0000 1010 == 10
 Shift two place to the left (*4)
 0001 0100 == 20
Binary Shift
 5 == 0101
 Shift one place to the right (/2)
 0010 -> 2
 Shift two place to the right (/4)
 0001 -> 1
 Shift one place to the left (*2)
 1010 -> 10
 Shift two place to the left (*4)
 0100 -> 4
Measurement of memory 内存 size

 The byte 字节 is the smallest unit of


memory in a computer.
 1 byte 字节 = 8 bits 比特
 Some computers use larger bytes,
 such as 16-bit systems and 32-bit
systems, but they are always multiples
of 8.
Decimal Prefix

Equivalent denary value


Name of memory size
(bytes)
1 kilobyte (1 KB) 1 000
1 megabyte (1 MB) 1 000 000
1 gigabyte (1 GB) 1 000 000 000
1 terabyte (1 TB) 1 000 000 000 000
1 petabyte (1 PB) 1 000 000 000 000 000

The system of numbering shown in Table 1.1 only


refers to some storage devices, but is technically
inaccurate. It is based on the SI (base 10) system
since memory size is actually measured in
terms of powers of 2, another system has
been proposed by the International
Electrotechnical Commission (IEC); it is
based on the binary system. See Table 1.2.
Binary Prefix

Equivalent denary value


Name of memory size Number of bytes
(bytes)
1 kibibyte (1 KiB) 210 1 024
1 mebibyte (1 MiB) 220 1048576
1 gibibyte (1 GiB) 230 1 073 741 824
1 tebibyte (1 TiB) 240 1 099 511 627 776
1 pebibyte (1 PiB) 250 1 125 899 906 842 624

This system is more accurate. Internal memories (such as RAM)


should be measured using the IEC system. A 64GiB RAM could,
therefore, store 64 × 230 bytes of data (68 719 476 736 bytes).
9618_s21_qp_11 1a
1.1.3 Hexadecimal number system

Hexadecimal, memory dump


 One problem with binary though, it can get very long, very difficult to read, very
quick!
 Ex. To represent 1024 we need 10 digits!

 What should we do?


Hexadecimal 十六进制

Why hexadecimal?

Hexadecimal essentially combines each four binary bits


into one hexadecimal digit.
 Shorter way of representing binary
 takes up less space
 Easier to read/write/understand
 Easier to debug 调试 and locate errors in data
Hexadecimal
A base-16 numbering system
Uses 16 digits:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
A(10), B(11), C(12), D(13), E(14), F(15)
Hexadecimal
Hexadecimal
Hexadecimal
• Each digit in a hexadecimal number represents a power of 16

Value 4096 256 16 1


Heading 163 162 161 160
Hexadecimal
• What is 92 in hexadecimal in denary?

ANS = 9x16+2x1 = 146


Hexadecimal to denary
 Please convert E5(hexadecimal) to denary representation
Hexadecimal to denary
 Please convert E5(hexadecimal) to denary representation

16 1
161 160
E = 14 5

(14 x 16) + (5 x 1) = 224 + 5 = 229


Binary -> Hexadecimal
Two ways:
1. Binary -> Decimal -> hexadecimal
2. Convert every 4 bits into 1 hex digits from right to
left(Recommended)
Please convert 0110 1110 to hexadecimal

8 4 2 1 8 4 2 1
0 1 1 0 1 1 1 0

Step1: Group every 4 bits together


Please convert 0110 1110 to hexadecimal

8 4 2 1 8 4 2 1
0 1 1 0 1 1 1 0

4+2 = 6 8+4+2=E(14)

Step1: Group every 4 bits together


Step2: Convert each group into one hexadecimal digits
Please convert 0110 1110 to hexadecimal

8 4 2 1 8 4 2 1
0 1 1 0 1 1 1 0
4+2 = 6 8+4+2=E(14)
ANS = 6E
Step1: Group every 4 bits together
Step2: Convert each group into one hexadecimal digits
Step3: Combine hex digits
Binary -> Hexadecimal
• Note: If there are not enough digits to make a group of 4, add
leading zeros as needed.
Hexadecimal -> Binary
 1. Hex -> Denary -> Binary
 2. Convert each hex digit into 4 bits (Recommended)

 Please convert A2(Hexadecimal) to Binary representation


Hexadecimal -> Binary
 Please convert A2(Hexadecimal) to Binary representation

A = 10 2
8 4 2 1 8 4 2 1
Hexadecimal -> Binary
 Please convert A2(Hexadecimal) to Binary representation

A = 10 2
8 4 2 1 8 4 2 1
1 0 1 0 0 0 1 0

ANS = 1010 0010


Denary -> Hexadecimal

Please write 1616 in Hexadecimal


1. Modulus
2. Denary -> Binary -> Hexadecimal
Please write 1616 in Hexadecimal

Divisor Divident & Remainder


quotient
1616
16 101 0
16 6 5
16 0 6

ANS = 650
Denary to hexadecimal
 Please convert 57 (Denary) to Hexadecimal representation
 Denary -> Binary -> Hexadecimal
Memory dumps
 Hexadecimal is often used when developing new software or when
trying to trace errors in programs.
 When the memory contents are output to a printer or monitor, this is
known as a memory dump.
Memory dumps

A program developer can look at each of the hexadecimal codes


(as shown in Table 1.4) and determine where the error lies.
The value on the far left shows the memory location, so it is
possible to find out exactly where in memory the fault occurs.

Using hexadecimal is more manageable than binary.


Where is it used?
 IP Address: "192.168.0.1" == "C0.A8.00.01"
 MAC Addresses
 Memory Addresses
 Error messages & debugging
 Color code
 ASCII/Unicode: The letter "A" in ASCII is 65
(denary), 41(hex)
 URL: my%20website.com/page%20one, %20 ==
space char
 Assembly language…
IP Address

IP stands for "Internet Protocol," which is the set of rules


governing the format of data sent via the internet or local
network.
In essence, IP addresses are the identifier that allows
information to be sent between devices on a network: they
contain location information and make devices accessible for
communication.
MAC(Media Access Controll) Address

 12 digits hexadecimal address


 Used to identifies device
 Unique for each device
 Embedded into device by manufacturer
 The first part is Manufacturer ID/number/identifies the manufacturer
 The second part is the serial number
 It’s static
Memory Address 内存地址

In computing, a memory address


is a reference to a specific
memory location used at various
levels by software and
hardware.
Error code 错误代码

In computing, an error code


(or a return code) is a
numeric or alphanumeric
code that indicates the
nature of an error and, when
possible, why it occurred.
Colour Code

 Color codes are ways of


representing the colors in a
format that a computer can
interpret and display. Commonly
used in websites and other
software applications, there are a
variety of formats, including Hex
color codes, RGB and HSL
values, and HTML color names,
amongst others.
3B
1.1.4 Binary Coded Decimal (BCD)
system
The binary-coded decimal (BCD) system uses a 4-bit
(nibble) binary code to represent each denary digit:
Therefore, the denary number 3 1 6 5 would be
0011 0001 0110 0101
in BCD format.
 The 4-bit code can be stored in the
computer either as
 half a byte
 or
 two 4-bit codes stored together to
form one byte, aka packed BCD
 For example, using 3 1 6 5 again ...
What is 8503 in BCD representation and packed BCD
representation?

Step1: work out 4-bit representation for each digit


Step2: combine them together
One BCD digit per byte:
 00001000 00000101 00000000 00000011
Two BCD digits per byte:
 10000101 00000011
Uses of BCD

The most obvious use of BCD is in the representation of


digits on a calculator or clock display, even currency value.

Each denary digit will have a BCD equivalent value which


makes it easy to convert from computer output to denary
display.
What about decimals?
Imagine adding $0.37 and $0.94 together using fixed-point
decimals.
(i.e. each digit represented in binary)
. 3 7
0 0 1 1 0 1 1 1

. 9 4
+ 1 0 0 1 0 1 0 0
1 1 1
1 1 0 0 1 0 1 1
12 11

0.1211? WRONG ANSWER


. 3 7
0 0 1 1 0 1 1 1

. 9 4
+ 1 0 0 1 0 1 0 0
1 1 1
1 1 0 0 1 0 1 1
12 11

What happened?

11’s 1 should be carried over to the first denary digit.


Solution: add 0110(6) whenever this issue arised
If we look at the
example again, we can
add .07 and .04 (the
two digits in the
second decimal place)
first.
Now we will add .3 and .9
together (the two digits in
the first decimal place)
remembering the carry bit
from the addition above:
This produces 1 1 0 1 which
isn’t a denary digit; this will
flag an error and the
computer again needs to add
0 1 1 0.

You might also like