1 Data Representation
IGCSE

1 Data Representation

1.1 Number systems1.1.1 Binary represents dataany form of data needs to be converted into a binary f...

花野猫

花野猫

更新于 2024-01-17

10031

1.1 Number systems

1.1.1 Binary represents data

any form of data needs to be converted into a binary format so that it can be processed by the computer.

The basic building block in all computers is the binary number system. This system is chosen because it only consists of 1s and 0s. Since computers contain millions and millions of tiny ‘switches’, which must be in the ON or OFF position, they can be represented by the binary system.

  • A switch in the ON position is represented by 1;
  • A switch in the OFF position is represented by 0.

switches represented by binary system
switches represented by binary system

1.1.2 Binary, denary and hexadecimal systems

The binary system

5432
5 ×1000+4 ×100+3 ×10+ 2 ×1
5×103+4×102+3×101+2×1005×10^3+4×10^2+3×10^1+2×10^0
  • Denary uses ten separate digits, 0-9, to represent all values.
  • Denary number system counts in multiples of 10. This gives us the well-known headings of units (计数单位), 10s, 100s, 1000s, and so on.
  • Denary is known as a base 10 number system.
  • The binary number system is a base 2 number system. It is based on the number 2. Thus, only the two ‘values’ 0 and 1 can be used in this system to represent all values. Using the same method as denary, this gives the headings 20,21,22,23, and so on.
denarybinary
example54321011
value5×1000+4×100+3×10+2×11×8+0×4+1×2+1×1
digits0-90-1
typebase 10 number systembase 2 number system
headings of units100,101,102,103, ...20,21,22,23, ...

binary to denary

Take each of the digits and multiply it by the heading values. Add all the resultant totals together to give the denary number.

example:

  1. Convert the binary number, 11101110, into a denary number.
272^7262^6252^5242^4232^3222^2212^1202^0
11101110

Denary Number = 128 + 64 + 32 + 0 + 8 + 4 + 2 + 0 = 238


exercise:

Convert the binary number, 011110001011, into a denary number. 1931

denary to binary

Method 1

The denary number 142 is made up of 128 + 8 + 4 + 2:

  • 142 – 128 = 14;
  • 14 – 8 = 6;
  • 6 – 4 = 2;
  • 2 – 2 = 0;

in each stage, subtract the largest possible power of 2 and keep doing this until the value 0 is reached.

128 + 8 + 4 + 2
1 0 0 0 1 1 1 0

Method 2

successive division by 2

  1. Start with the denary number, and divide it by 2.
  2. Write the result of the division including the remainder (even if it is 0)
  3. then divide the quotient again by 2 and keep dividing until the result is zero.
  4. Finally write down all the remainders in reverse order

59 = 1 1 1 0 1 1


exercise:

Convert the following denary numbers into binary (using both methods):

  • 67
  • 127
  • 189
  • 33000
  • 4095
  • 62307

The hexadecimal system

Hexadecimal (sometimes referred to as simply ‘hex’) is a base 16 system and therefore needs to use 16 different ‘digits’ to represent each value. the letters A to F are used to represent each hexadecimal (hex) digit:

Binary valueHexadecimal valueDenary value
000000
000111
001022
001133
010044
010155
011066
011177
100088
100199
1010A10
1011B11
1100C12
1101D13
1110E14
1111F15

The hexadecimal number system is very closely related to the binary system. Since 16=2416 = 2^4 this means that FOUR binary digits are equivalent to each hexadecimal digit.

hexadecimal
example21F3A
value2×65536+1×4096+15×256+3×16
digits0-F
typebase 16 number system
headings of units160,161,162,163, ...

binary to hexadecimal
  1. 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.
  2. Take each group of 4 bits and convert it into the equivalent hexadecimal digit
1 0 1 1 1 1 1 0 0 0 0 1
1 0 1 1 1 1 1 0 0 0 0 1
B E 1

exercise:

  • 1 0 0 0 0 1 1 1 1 1 1 1 0 12 1 F D
  • 1 0 0 0 1 0 0 1 1 1 1 0
  • 0 0 1 0 0 1 1 1 1 1 1 1 0

hexadecimal to binary
  1. take each hexadecimal digit
  2. write down the 4-bit code which corresponds to the digit.
45A
0 1 0 0 0 1 0 1 1 0 1 0
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.

the hex digits A → F need to be first converted to the values 10 → 15 before carrying out the multiplication.


exercise:

  • 45A1114
  • C8F3215

denary to hexadecimal

Successive division by 16 until the value 0 is reached. Write the result of the division including the remainder (even if it is 0) The hexadecimal number is obtained from the remainders written in reverse order.


exercise:

  • 20047D4
  • 8463210F

1.1.3 Use of the hexadecimal system

  • a computer can only work with binary data.
  • hexadecimal to be more convenient to use. One hex digit represents four binary digits: A complex binary 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.

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.

MAC addresses

Media Access Control (MAC) address refers to a number which uniquely identifies a device on a network.

  • refers to the network interface card (NIC) which is part of the device.
  • rarely changed so that a particular device can always be identified no matter where it is.
  • usually made up of 48 bits which are shown as 6 groups of two hexadecimal digits (although 64-bit addresses also exist): NN – NN – NN – DD – DD – DD or NN:NN:NN:DD:DD:DD
  • Very often lowercase hexadecimal letters are used in the MAC address: 00-1c-b3-4f-25-fe.
first half (NN-NN-NN)second half (DD-DD-DD)
valueIdentity Number of the manufacturerSerial Number of the device
example00-1C-B3 (Apple Corporation )4F-25-FE

IPv6 addresses

Each device connected to a network is given an address known as the Internet Protocol (IP) address. 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. Note IPv6 uses a colon (:) rather than a decimal point (.) as used in IPv4.

TypeIPv4IPv6
Description32-bit number128-bit number
RepresentationDecimal or hexadecimalHexadecimal
Example109.108.158.1
(or 77.76.9e.01 in hex)
a8fb:7a88:fff0:0fff:3d21:2085:66fb:f0fa

HTML colour codes

HyperText Mark-up Language (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.

example of HTML code:

html
<h1 style="color:#FF0000;">This is a red heading</h1>
<h2 style="color:#00FF00;">This is a green heading</h2>
<h3 style="color:#0000FF;">This is a blue heading</h3>

HTML is often used to represent colours 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. For example:

  • #FF8000 represents orange

The # symbol always precedes hexadecimal values in HTML code. The colour codes are always six hexadecimal digits representing the red, green and blue components. 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.

1.1.4 Addition of binary numbers

Carry whenever the sum is greater than 1.


overflow

in the addition of two 8-bit positive binary numbers, the maximum denary value of an 8-bit binary number is 255 (which is 282^8 – 1). The generation of a 9th bit indicates that the sum has exceeded this value. This is known as an overflow error and in this case is an indication that a number is too big to be stored in the computer using 8 bits.

The greater the number of bits which can be used to represent a number then the larger the number that can be stored:

register typeMaximum Denary Value
16-bit register65 535 (21612^{16} – 1)
32-bit register4 294 967 295 (23212^{32} – 1)

1.1.5 Logical binary shifts

Computers can carry out a logical shift on a sequence of binary numbers. It means moving the binary number to the left or to the right.

Shift OperationEffect
Shift Leftmultiplying the binary number by 2
Shift Rightdividing the binary number by 2

As bits are shifted, any empty positions are replaced with a zero.

There is clearly a limit to the number of shifts which can be carried out if the binary number is stored in an 8-bit register:

The denary number 21 is 00010101 in binary. If we put this into an 8-bit register:if we shift the original binary number 00010101 four places left: The left-most 1-bit has been lost. In our 8-bit register the result of is 80 which is incorrect. This error is because we have exceeded the maximum number of left shifts possible using this register.

Decimal NumberBinary NumberDenary Result
2100010101-
Shifted 1 left0010101084 (21× 22)
Shifted 2 left0101010084 (21× 222^2)
Shifted 3 left10101000168 (21× 232^3)
Shifted 4 left0101000080 (error)

1.1.6 Two’s complement (binary numbers)

To allow the possibility of representing negative integers we make use of two’s complement. 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

when applying two’s complement to a binary number the left-most bit always determines the sign of the binary number. A 1-value in the left-most bit indicates a negative number and a 0-value in the leftmost bit indicates a positive number

1.2 Text, sound and images

  • The ASCII code system (American Standard Code for Information Interchange)
  • Extended ASCII
  • Unicode
-ASCIIExtended ASCIIUnicode
year of publish1963
newer version in 1986.
-1991
numbers of bit7bit8bit16-bit or 32-bit
notedoes not represent characters in non-Western languages, for example Chinese characters.DOS and Windows use different characters for some ASCII codes.can represent all languages of the world, thus supporting many operating systems, search engines and internet browsers used globally.
character set● consists of 7-bit codes (0 to 127 in denary or 00 to 7F in hexadecimal) that represent the letters, numbers and characters found on a standard keyboard, together with 32 control codes (that use codes 0 to 31 (denary) or 00 to 19 (hexadecimal)).
● the sixth bit changes from 1 to 0 when comparing the lowercase and uppercase of a character.
● the character sets (e.g. a to z, 0 to 9, etc.) are grouped together in sequence, which speeds up usability.
● uses 8-bit codes (0 to 255 in denary or 0 to FF in hexadecimal). This gives another 128 codes to allow for characters in non-English alphabets and for some graphical characters to be included● There is overlap with standard ASCII code, since the first 128 (English) characters are the same.
● ASCII uses one byte to represent a character, whereas Unicode will support up to four bytes per character.

The Unicode consortium was set up in 1991. Version 1.0 was published with five goals; these were to:

  • create a universal standard that covered all languages and all writing systems
  • produce a more efficient coding system than ASCII
  • adopt uniform encoding where each character is encoded as 16-bit or 32-bit code
  • create unambiguous encoding where each 16-bit and 32-bit value always represents the same character
  • reserve part of the code for private use to enable a user to assign codes for their own characters and symbols (useful for Chinese and Japanese character sets, for example).