# SCD - 1.4.1 Data Types

- Created by: Freddie Frame
- Created on: 19-09-20 15:26

## T1 - Primitive Data Types, Binary and Hexadecimal

Primitive data type is provided by a programming language that are all held in binary and includes:

- Integers - Whole Integers
- Real/Float - Fractional Numbers
- Boolean - True or False
- Character - Letter, Number or Special Symbol
- String - Anything encased in quote marks

Denary is refered to as base-10 (digits 0-9), binary is base-2 (0-1) and hex is base-16 (0-15)

Hex number system goes from 1-9 and then A-F (10-15)

e.g. 183 in hex - convert to binary = 1011 0111 then into hex, 1st part = 11 = B, 2nd part = 7 = 7 therefore 183=B7

## T2 - Number Systems, ASCII and Unicode

Transistors are used to turn voltages on and off that act as switches where ON = 1 and OFF = 0

Inidvidual digit in a binary value is called a **bit, **these can then be combined into groups of 8 called **bytes**, for larger values numerical prefixes are applied to bytes

- Kilobyte - kB = 1,000
- Megabyte - MB = 1,000,000

However nowadays a base-2 multiplier is used instead of a base-10 (above) meaning that 1kB = 1024 Bytes which warrant a new set of prefixes

- Kibibyte - KiB = 1,024
- Mebibyte - MiB = 1,048,576

## T2 - ASCII and Unicode

**ASCII** - American Standard Code for Information Interchange, ASCII was established to encode symbols in the english alpabet using a **7-bit character set** giving 128 possible codes, due to this limit other languages and symbols cannot be represented. Symbols, uppercase and lowecase letters have their own unique values

Extended ASCII has an 8th bit added for extra characters/symbols (256 possible codes)

**Unicode** - Standardises encoding of characters from all languages which is done using either **16 or 32 bits**, to improve adoption, the first 128 characters are the same as in the ASCII character set

Using Unicode means that there can be up to 4 bytes per character so file sizes are going to be much larger than using ASCII (4x). This however means that every character in every language, and every mathematical and scientific symbol, etc. can be represented.

## T3 - Binary Arithmetic

When adding two binary numbers together, if a number is too large (1 already in the place) then the next bit is carried over, these are called **carry bits** and need to be shown above the calculation

As computers used a fixed number of bytes then an **overflow error** can occur when the result of the addition is too large for the nuumber of bits the computer works with.

Range of bits = 2^{x} (X = number of bits) - 1 e.g. 2^{4} - 1 = 15

**Sign and Magnitude** - Used to represent negative numbers in binary where the first bit acts as a **sign bit where 0 = + and 1 = -** , however binary arithmetic does not work when using this method

**Two's Complement** - used to represent negative values, similar to an analogue clock when turning back wheel from 1 it will equal 9999, a one in the first bit in 8-bit number = -128, used to ensure that subtraction occurs when adding negative values

Two's complement of a signed binary value (negative) is found by flipping all of the bits and adding one. Example: 77 = 01001101, flip = 10110010. add 1 = 10110011 = -77

This reduces the range, halving it. Maximum for 8 bits is 127 (01111111) and minimum is -128 (10000000)

## T3 - More on Two's Complement & Binary Fractions

Two's complement can also encounter an overflow error when the result is too large to fit in the allocated number of bits as if it requires more bits the number will be incorrectly represented as a 1 in the leftmost bit inicates a negative number

**Binary Fractions** - Bits to the right of the notional point (indicates switch to fractions) indroduces fractional values represented in negative powers of 2 (0.5 > 0.25 > 0.0125 > etc. halving each time)

**Fixed Point Binary** - Uses a specified number of bits where the placement of the binary value is fixed e.g. if point is placed inbetween 4th and 5th bits in an 8-bit number then 6.625 as a fixed point binary would be 0110.1010, **binary point can be placed anywhere**

This increases accuracy as fractional points can be used but decreases range as the number of bits is still limited

## T4 - Floating Point Arithmetic

**Floating Point Numbers** - Used with very large decimal values, held in the format Mx10^{n} (M = mantissa, n = exponent)

12 bits are commonly used with the leftmost bit being a **sign bit**, the seven to the right being the **mantissa** (binary value) and the last 4 being the **exponent** (power)

- Example - 0.1101100 0100 into denary assuming an 8-bit mantissa and 4-bit exponent:
- Mantissa = 0.1101100 , Exponent = 0100 = 4 therefore binary point of mantissa is moved 4 places to the right, multiplying it by 2
^{4} - Bianry Number = 01101.100 = 13.5

Floating point numbers can also be negative if they have a sign bit as one, in this case you would convert the mantissa into two's complement form and then apply the exponent

- Example: 1.0010100 0011
- Mantissa = 1.00101100 > 0.1101011 +1 = 01101100 in two's complement form
- Exponent = 3, therefore binary point is moved 3 places to the right
- 0110.1100 = -6.75
**Don't forget to add back the negative sign**

## T4 - Negative Exponents & Noramlisation

Like in the mantissa the **1st bit of the exponent is a sign bit** so a 4-bit exponent of 1000 is -8, to calculate the value of a negative exponent add the numbers so 1111 = -8 + 4 + 2 + 1 = -1

**Positive Exponent**- Binary point moved to the right**Negative Exponent**- Bianry point moved to the left

Example: 0.1100000 1110 to denary

- Mantissa = 0.1100000 | Exponent = 1110 = -8 + 6 = -2
- Binary point moved 2 places to the left = dividing by 4 or 2
^{2}or multiplying by 2^{-2} - Binary Number = 0.0011000 = 0.125 + 0.0625 = 0.1875

**Normalisation** - Moving the binary point of a floating point number to provide maximum precision for a given number of bits. Example of this process - 0.1234567 x10^{7} = 1,234,567 whereas 0.0012345 x10^{9} = 1,234,500 < To normalise shift by 2 digits

In normalised floating point form:

- A positive number that has a sign bit of 0
**MUST**have the next digit as a 1 - A negative number that has a sign bit of 1
**MUST**have the next digit as a 0

## T4 - Normalising Postivive and Negative Binary

Example: Normalise the binary number 0.0011010 0100

- Binary point needs to move 2 places to the right so that there is a 1 following the binary point
- Pad with 0s at the righthand end, giving 0.1101000
- To compensate for the larger mantissa, exponent must be made smaller
- Subtract 2 from the exponent making it 0010
- Normalised number is 0.1101000 0010

**Normalising Negative Numbers** - Follows the same process as normalising a positive binary number with the only difference being the a 0 MUST follow the 1 sign bit

- Example: Normalise the number 1.1100011 0011
- Move binary point 2 places right and subtract 2 from exponent = 111.00011 0001 which is the same as 1.0001100 0001

The number of 1's before a binary point in a negative number does not matter which is the same for leading 0's before a binary point in a positive number

## T4 - Denary to Normalised Floating Point Binary

Example 1: 88 = 01011000

- Move binary point 7 places left = 0.1011000 and set exponent equal to 7 = 0111
- Number = 0.1011000 0111

Example 2: 17.25 = 010001.01

- Move binary point 5 places left = 0.1000101 and set exponent equal to 5 = 0101
- Number = 0.1000101 0101

**For a negative number calculate two's complement before normalising**

Example: -17.75

- 17.75 = 01001.11 > 101110.00 > 101110.01
- Move point 5 places left = 1.0111001 and set exponent equal to 5 = 0101
- Number = 1.0111001 0101

## T4 - Adding Normalised Numbers with Different Mans

For numbers that have different mansissa that need to be added, they need to have their mantissas equalised

Example: A =0.1100000 0001 B = 0.1111100 0011

**Convert to fixed point and add**: A = 1.1000 B = 111.1100 so sum = 1001.0100- As number is positive, sign bit will be zero
- convert to normalised floating point = 0.1001010 0100

If needing to subtract a number from the other, find the two's complement of the number to be subtracted and then add together

## T5 - Bitwise Manipulation and Masks

**Logical shifts** are used to examine the least/most significant bit, after operation the carry bit can be tested and a conditional branch executed

**Logical Shift Right**- Least significant bit (1st bit) shifted right into carry bit and a zero moves into the new space on the left (Dividing by 2 - Reduces accuracy)**Logical Shift Left**- Most significant bit (8th bit) shifted left into carry bit and a zero moves into the new space on the right (Multiplying by 2)

**Arithmetic shifts** are used to multiply and divide by multiples on 2, by combining them two binary numbers can be multiplied together. When dividing a nymber by 2 it rounds down, this reduces accuracy as calculations such as 3/2 would equal 1

**Arithmetic Shift Right**- Same as logical shift right except for the new space has a 1 instead**Arithmetic Shift Left**- Sign bit preserved and instead shifts the 2nd digit on the left into the carry bit where a zero then moves into the new space on the right

Example: Multiply 17 by 5 using shifts and addition

- Multiply 17 by 1 = 0001 0001
- Multiply 17 by 4 with 2 left shifts = 0100 0100
- Add together = 0101 0101 = 85

## T5 - Circular Shifts

**Circular Shift Right with Carry** - Carry bit is moved into the most significant bit (rightmost bit) and the least significant bit (leftmost bit) is moved into the carry bit (All bits move 1 bit to the right)

**Circular Shift Left with Carry** - Value in most significant bit is moved into the carry bit and the carry bit is moved into the least signinicant bit (Opposite process to right shift)

Example: Binary number 10011011 - arithmetica shift right 2 places then circular shift right by 1 place and multiply by 6

- Arithmetic shift right by 2 places: 10011011 > 11100110 1
- Circular shift right by 1 place: 11100110 1 > 11110011 0
- Multiply by 4 = 1111001100
- Multiply by 2 = 111100110
- Add together = 10110110010 = 1458

## T5 - Masks

Logical Instructions:

**AND**- Both statements must be true for the condition to be true**OR**- Only one statement has to be truefor the condition to be true**XOR**(Exclusive OR) - True if either input is true but not both

**Masks** - Used to manipulate bits by having an input which changes the output of the binary

**AND**- Used to clear a particular bit, leaving others unchanged, use a mask of 0 to clear a bit (make it a 0) and a mask of 1 to leave the bit unchanged**OR**- Used to set a particular bit, leaving others unchanged, use a mask of 1 to set a bit (make it a 1) and a mask of 0 to leave it unchanged**XOR**- Used to toggle a particular bit, leaving others unchanged, use a mask of 1 to toggle a bit (flip it to the other bit) and a mask of 0 to leave it unchanged

## Related discussions on The Student Room

- computer sciensce »
- Cambridge iGCSE Computer Science Paper 1 »
- Stata - dependent binary variable »
- AQA A-level Computer Science Paper 2 (7517/2) - 18th June 2024 [Exam Chat] »
- Eduqas A-Level Computer Science Component 1 (10th of June 2024) »
- OCR GCSE Computer Science Paper 1 (J277/01) - 19th May 2023 [Exam Chat] »
- Scheduling problem »
- OCR A-level Computer Science Paper 1 (H446/01) - 12th June 2023 [Exam Chat] »
- Interviews Research »
- OCR A-level Computer Science Paper 2 (H446/01) - 18th June 2024 [Exam Chat] »

## Comments

Report