0% found this document useful (0 votes)
32 views68 pages

Data Representation

Computer science
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)
32 views68 pages

Data Representation

Computer science
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/ 68

1.

Data Representation

O-Level Computer Science


Course Code: 2210
Class: Sr-I
Engro Grammar School
1.1 Number systems

1.1.1 Binary represents data


1.1.2 Binary, denary, and hexadecimal number systems
• The binary system
• Converting from Binary to Denary (two methods)
• Converting from Denary to Binary (two methods)
• The hexadecimal system
• Converting from Binary to Hexadecimal and from Hexadecimal to Binary
• Converting from Hexadecimal to Denary and from Denary to Hexadecimal
1.1 Number systems

1.1.3 Use of hexadecimal system


• Error codes
• MAC address
• IP address
• HTML color codes
1.1.4 Addition of binary numbers
• Overflow
1.1.5 Logical binary shifts
1.1 Number systems

1.1.6 Two’s complement (binary numbers)


• Writing positive binary numbers in two’s complement format
• Converting positive denary numbers to binary numbers in two’s complement
format
• Converting positive binary numbers to positive denary numbers in two’s
complement format
• Writing negative binary numbers in two’s complement format and converting
to denary
• Converting negative denary numbers into binary numbers in two’s
complement format
1.1.1 Binary Represents Data
Definition: A number system that uses only two digits, 0 and 1.
Base: It is a base-2 number system, unlike the decimal system which is base-10.
It uses only two digits (0 and 1) to represent all other values or numbers.

Why is Binary Important in Computers?


Representation: Computers use binary to represent all types of data, including
numbers, text, images, and sounds.
Digital Signals: Binary corresponds to the on (1) and off (0) states of digital signals.
Switches: Computers contain millions and millions of tiny switches which must be
in ON (1) or OFF (0) states. Switches are made up of logic gates.
1.1.1 Binary Represents Data
How Binary Represents Data
Bits and Bytes:
• A bit (Binary digit) is the smallest unit of data in a computer, either 0 or 1.
• A byte consists of 8 bits. Example: 10101010.

• Binary numbers are crucial for data representation in computers.


• They use only two digits: 0 and 1.
• Computers process data in binary to perform various operations
1.1.2 Binary, Denary, and Hexadecimal Systems
- The Binary Number System
The Denary Number System
1.1.2 Binary, Denary, and Hexadecimal Systems
- The Binary Number System
1.1.2 Binary, Denary, and Hexadecimal Systems
- Converting from Binary to Denary
Each time a 1-value appears in a binary number column, the column value
(heading) is added to a total.
- Converting from Denary to Binary
Method 1:
The first method involves successive subtraction of powers of 2
(that is, 128, 64, 32, 16, and so on)

Method 2:
The second method involves successive division by 2
until the value “0” is reached.
- Converting from Denary to Binary
- Converting from Denary to Binary
1.1.2 Binary, Denary, and Hexadecimal Systems
- The Hexadecimal Number System
What is the Hexadecimal Number System?

• Definition: A number system that uses sixteen distinct symbols.


• Base: It is a base-16 number system.
• Symbols: 0-9 and A-F (where A=10, B=11, C=12, D=13, E=14, F=15).

Why is Hexadecimal Important in Computers?


Compact Representation: It is more compact and easier to read
Usage: Used in programming, memory addresses, and color codes in web design.

It provides a more human-friendly way to represent binary-coded values.


1.1.2 Binary, Denary, and Hexadecimal Systems
- The Hexadecimal Number System
Hexadecimal Conversions

1) Binary to Hexadecimal
2) Hexadecimal to Binary

3) Hexadecimal to Denary
4) Denary to Hexadecimal
- Converting from Binary to Hexadecimal
Starting from the right and moving left, split the binary number
into groups of 4 bits.

If the last group has less than 4 bits, then simply fill in with 0s
from the left.

Take each group of 4 bits and convert it into the equivalent


hexadecimal digit using Table 1.1.
- Converting from Hexadecimal to Binary

Using the data in Table 1.1

Simply take each hex digit


and
write down the 4-bit code
which corresponds to the digit.
- Converting from Hexadecimal to Denary
Take each of the hexadecimal digits and multiply it by the heading values.
Add
all the resultant totals together to give the denary number.

Remember that:

Hex digits A → F need to be first converted to the values 10 → 15 before


carrying out the multiplication.
- Converting from Denary to Hexadecimal

Involves successive division by 16


until the value “0” is reached.
1.1.3 Use of Hexadecimal System

• A computer can only work with binary data.


• Computer scientists can work with binary, they find hexadecimal to be more
convenient to use.
• This is because one hex digit represents four binary digits.
• A complexbinary number, such as 1101001010101111 can be written in hex as
D2AF.
• The hex number is far easier for humans to remember, copy, and work with.
1.1.3 Use of Hexadecimal System

» Error codes
» MAC addresses
» IPv6 addresses
» HTML colour codes
1. Error Codes
• Error codes are often shown as hexadecimal values.
• These numbers refer to the memory location of the error and are usually
automatically generated by the computer.
• The programmer needs to know how to interpret the hexadecimal error codes.

• Examples of error codes from a Windows system are shown below:


2. Media Access Control (MAC) Addresses
• It refers to a number that uniquely identifies a device on a network.
• The MAC address refers to the network interface card (NIC) which is part of
the device.
• The MAC address is rarely changed so that a particular device can always
be identified no matter where it is.
• A MAC address is usually made up of 48 bits which are shown as 6 groups
of two hexadecimal digits (although 64-bit addresses also exist):

The first half (NN – NN – NN) is the identity number of


the manufacturer of the device
and
The second half (DD – DD – DD) is the serial number of
the device.
3. Internet Protocol (IP) Addresses
• Each device connected to a network is given an address known as the Internet
Protocol (IP) address.
• An IPv4 address is a 32-bit number written in denary or hexadecimal form:

109.108.158.1 (in binary) or (77.76.9e.01 in hex).

• IPv4 has recently been improved upon by the adoption of IPv6.


• An IPv6 address is a 128-bit number broken down into 16-bit chunks,
represented by a hexadecimal number.

a8fb:7a88:fff0:0fff:3d21:2085:66fb:f0fa
4. HyperText Mark-up Language Color Codes
• HTML is used when writing and developing web pages.
• HTML isn’t a programming language but is simply a mark-up language.
• A mark-up language is used in the processing, definition and presentation of text
(for example, specifying the colour of the text).
• HTML uses <tags> which are used to bracket a piece of text for example, <h1>
• and </h1> surround a top-level heading.
• Whatever is between the two tags has been defined as heading level 1. Here is a
short example of HTML code:
• HTML is often used to represent colors of text on the computer screen.
• All colours can be made up of different combinations of the three primary colours
• (red, green and blue).
• The different intensity of each colour (red, green and blue) is determined by its
hexadecimal value.
• This means different hexadecimal values represent different colours.
• There are a possible 256 values for red, 256 values for green and 256 values for
blue giving a total of 256 × 256 × 256 (i.e. 16 777 216) possible colours.

For example:
» # FF 00 00 represents primary colour red
» # 00 FF 00 represents primary colour green
» # 00 00 FF represents primary colour blue
» # FF 00 FF represents fuchsia
» # FF 80 00 represents orange
» # B1 89 04 represents a tan colour
1.1.4 Addition of Binary Numbers
The addition of two 8-bit positive binary numbers
Overflow
• This addition has generated a 9th bit. The 8 bits
of the answer are 0 1 0 0 1 1 0 0
• This gives the denary value (64 + 8 + 4) of 76
which is incorrect because the denary value of
the addition is 110 + 222 = 332.
• The maximum denary value of an 8-bit binary
number is 255 (which is 28 – 1).
• The generation of a 9th bit is a clear indication
that the sum has exceeded this value.

• This is known as an overflow error and shows


that the answer number is too big to be stored in
the computer using 8 bits.
Overflow
• The greater the number of bits that can be used to represent a
number then the larger the number that can be stored.

• For example, a 16-bit register would allow a maximum denary value


of 65 535 (i.e. 216 – 1) to be stored, a 32-bit register would allow a
maximum denary value of 4 294 967 295 (i.e. 232 – 1), and so on.
1.1.5 Logical Binary Shift

• Computers can carry out a logical shift on a sequence of binary numbers.


• The logical shift means moving the binary number to the left or to the right.
• Each shift left is equivalent to multiplying the binary number by 2 and each shift
right is equivalent to dividing the binary number by 2.
• As bits are shifted, any empty positions are replaced with a zero – see examples.
• There is a limit to the number of shifts which can be carried out.
1.1.5 Logical Binary Shift (cont.)

• if the binary number is stored in an 8-bit register.


• Eventually after a number of shifts the register would only contain zeros.
• For example, if we shift 01110000 (denary value 112) five places left (the
equivalent to multiplying by 25, i.e. 32), in an 8-bit register we would end
up with 00000000.
• This makes it seem as though 112 × 32 = 0! This would result in the
generation of an error message.
1.1.6 Two’s Complement (binary numbers)

• Up until now, we have assumed all binary numbers are positive integers.
• To allow the possibility of representing negative integers we make use of
two’s complement.
• In this section we will again assume 8-bit registers are being used.
• Only one minor change to the binary headings needs to be introduced
here:
1.1.6 Two’s Complement (binary numbers)
• In two’s complement the left-most bit is changed to a negative value.
• For instance, for an 8-bit number, the value 128 is now changed to −128, but all
the other headings remain the same.
• This means the new range of possible numbers is:
−128 (10000000) to +127 (01111111).
• It is important to realize when applying two’s complement to a binary number
that the left-most bit always determines the sign (+ or -) of the binary.
• A 1-value in the left-most bit indicates a negative number (00110011 = 51)
• A 0-value in the left-most bit indicates a posi ve number (11001111 = −49)
Writing positive binary numbers in two’s complement format
Converting positive denary numbers to binary numbers in the
two’s complement format
Converting positive binary numbers in the two’s complement
format to positive denary numbers
Writing negative binary numbers in two’s complement format
and converting to denary
Converting negative denary numbers into binary numbers in
two’s complement format

You might also like