1.4 Data Types and Structures Flashcards
What is a data type?
Formal classification of the type of data being stored or manipulated within a program to determine which operations can be performed on that data
What are primitive data types?
Common data types built into the language which can only contain one value
What are some examples of data types?
Integer
Float/real
Boolean
Char
String
What is an integer?
A positive or negative whole number
What is an example of an integer?
1
-98
38420
What is float/real?
A positive or negative number which can include decimal points
What is an example of a float/real?
1.849
-45.3
What is a Boolean?
Only one of True or False
What is an example of a Boolean?
True
False
What is a char?
A single character
What is an example of a char?
“a”
”#”
“5”
What is a string?
Not a primitive data type but a basic one in most languages
Collection of char data types
What is an example of a string?
“hello!”
“sh63£”
What is a composite/compound data type?
Built by combining primitive data types
What is an example of a composite/compound data type?
Record
Class
What is a data structure?
Collection of data organised to allow efficient processing
Lend themselves to different applications
Some highly specialised to specific tasks
What is an abstract data type?
Conceptual model
Describes how data is organised and which operations can be carried out on data
From perspective of end-user who doesn’t need to know how it’s implemented
What is binary?
Base 2
Uses 1s and 0s
List the forms of storage (e.g. bit, byte) from smallest to largest
Bit
Nibble
Byte
Kilobyte
Megabyte
Gigabyte
Terabyte
Petabyte
What is a bit?
A single 1 or 0
Smallest form of data storage in a computer
What is a nibble?
4 bits
What is a byte?
8 bits
2 nibbles
What is a kilobyte?
1000 bytes
What is a megabyte?
1000 KB
What is a gigabyte?
1000 MB
What is a petabyte?
1000 TB
What is a terabyte?
1000 GB
What is the largest number that can be stored by a nibble?
15
What is the largest number that can be stored by a byte?
255
What is 9 as a byte?
00001001
What is 102 as a byte?
01110011
What is hexadecimal?
Base-16
0-9 and A-F
What is each hex digit equivalent to?
A nibble
What are the uses of hex?
RBG = representing colours
MAC addresses
Memory dumps
What are the advantages of hex?
Easier to read and interpret
Fewer digits to represent the same value
Compared to binary, less likely that digit written down incorrectly
How do you convert denary to hex?
Convert into binary
Split into nibbles
Convert each nibble to hex
What is 87 in hex?
87 = 01010111 = 57
What is 210 in hex?
210 = 11010010 = D2
How do you convert hex to denary?
Convert all terms to denary (as per a nibble)
Times those numbers by 16 to the power of the nibble they are in minus 1 (if it’s the second nibble, times by 16^1)
Add numbers together
What is B9 in denary?
B9 = 176 + 9 = 185
What is 2CA in denary?
2CA = 512 + 192 + 10 = 718
What are two ways of representing negative numbers in binary?
Sign and magnitude
Two’s complement
What is sign and magnitude?
Changing left most bit (most significant bit) to represent if negative or not
0 = positive
1 = negative
What is -10 in sign and magnitude?
10001010
What is 103 in sign and magnitude?
01100111
What is two’s complement?
Changing left most bit (most significant bit) to present either 0 or -128
1 = -128
0 = 0
What is the method for converting a number to two’s complement?
Calculate number as positive binary value
Keep rightmost 1 and everything to the right of it the same
Flip all other value
What is -103 in two’s complement?
103 = 01100111
-103 = 10011001
What are the rules for binary addition?
1 + 0 = 1
1 + 1 = 0 carry 1
0 + 0 = 0
1 + 1 + 1 = 1 carry 1
What is 99 + 103 in binary?
99 = 01100011
103 = 01100111
01100011 \+ 01100111 ------------------- 11001010 ------------------- 11 111
= 11001010
What is the method for binary subtraction?
Convert numbers to two’s complement
Flip number you’re subtracting to a negative
Add numbers together
What is 75 - 36 in binary?
75 = 01001011
36 = 00100100
-36 = 11011100
01001011
+ 11011100
——————
100100111
——————-
1 11
= 00100111
What is fixed point binary?
Decimal point doesn’t move
Values before fixed point represent whole numbers
Values after fixed point represent fractions
Can be expressed using Two’s complement
What is the problem with fixed point binary?
Underflow - when number is too small to be represented
What is 5.75 in fixed point binary?
0101.1100
What is floating point binary?
Decimal place can be moved
Enables greater range of numbers and greater precision of numbers
More efficient way of storing binary numbers
What values are needed in floating point binary?
Mantissa
Exponent
What is the exponent?
Where the decimal point should move to
What is the mantissa?
Number being represented
Where does the binary point start?
Between the two leftmost bits
Which way should be binary point move in the exponent is positive?
Right
Which way should the binary point move if the exponent is negative?
Left
What should the bits be filled with when the exponent is negative?
If first number is 1, fill with 1s
If first number is 0, fill with 0s
What does normalised mean?
Standard form that should be used to represent numbers
Using normalisation, make sure digit after the binary point is SIGNIFICANT
What should normalised positive numbers start with?
01
What should normalised negative numbers start with?
10
What is overflow?
Where the number is too large a positive or negative to store
Crashes
What is underflow?
When the number is too small to be able to store
Crashes
What is the method of adding normalised form numbers?
Check if exponents are the same. If not, increase smaller exponent to match higher one
When doing this, need to adjust floating point on mantissa to the left, the number of places that you need to increase the exponent by
Add together just the mantissas using normal binary addition rules
What is the method of subtracting normalised form numbers?
Check if exponents are the same. If not, increase smaller exponent to match higher one
When doing this, need to adjust floating point on mantissa to the left, the number of places that you need to increase the exponent by
Negate the value to subtract
Add together just the mantissas using normal binary addition rules
What is a character set?
Standard collection of characters and the bit patterns used to represent them to allow computers to be able to communicate and exchange text efficiently
What does ASCII stand for?
American Standard Code for Information Interchange
What is ASCII?
Each character encoded with binary string of 7 bits
What is the 8th bit in ASCII often reserved for?
Error-checking/correction purposes
What is Unicode?
Uses more bits to store a character than ASCII, meaning a wider range of characters can be stored
What are the advantages of Unicode?
Can support languages with alphabet character sets larger than 26
Uses up to 32 bits to represent each character and can be used to represent special symbols
Supports over 4 billion possible codes
What version of ASCII is generally used?
UTF-16
What is bitwise manipulation?
Allows the programmer to work with sets of bits through logical bitwise operations
In both high and low level languages, used to perform a logical operation on each individual bit of a binary number
What are logical bitwise operations?
Directly supported by the processor, making them fast and efficient
Works on a single bit (or set of bits) within a binary string
Often used in low-level languages to manipulate the contents of registers or memory locations
What are the four logical bitwise operations?
AND
OR
XOR
NOT
What are binary or logical shifts?
Used to move binary digits
Can be used for arithmetic operations
What is a mask?
Bit pattern that’s been defined by a programmer, allowing specific bits in a piece of data to be tested or altered
Binary string used in conjunction with a logical bitwise operator to identify or manipulate a single bit (set of bits) within another binary string
What does the AND logical bitwise operation do?
Turn off/select bits
What is the result of AND operator applied to the binary string 10111100 using a mask of 11110000?
10110000
What does the OR logical bitwise operation do?
Turn on/select bits
What is the result of OR operator applied to the binary string 10111100 using a mask of 11110000?
11111100
What does the XOR logical bitwise operation do?
Reverse/toggle/complement/inverse
What is the result of XOR operator applied to the binary string 10111100 using a mask of 11110000?
01001100
What does applying shifts do?
Divide or multiply binary values
What are the uses of shifts?
Multiplying and dividing by a factor of 2
Encryption/decryption
Data collision detection in a network
Compression algorithms
Routing packets of data
Peripheral communication protocols
Graphics manipulation
How is a logical left shift completed?
- moving all the bits of the number one place to the left
- discarding the most significant bit
- putting a 0 into the empty place on the right
How is a logical right shift completed?
- moving all bits of the number one place to the right
- discarding the least significant bit
- putting a 0 into the empty place on the left
Can you logically shift signed numbers?
No
What type of shift should be used if the number is signed?
Arithmetic shift
What is a arithmetic shift?
Most significant bit is not shifted
Remaining bits are shifted
Sign of number is preserved and empty places are padded
What happens in an arithmetic left shift of a positive number?
Empty places are padded with 0
What happens in an arithmetic left shift of a negative number?
Empty places are padded with 0
What happens in an arithmetic right shift of a positive number?
Empty spaces are padded with 0
What happens in an arithmetic right shift of a negative number?
Empty places are padded with 1
What is a circular/cyclic shift?
No bits are lost
Bits are shifted out at one end into the other end of register
What are the uses of a cyclic shift?
In cryptography, cyclic shift of a code will always yield another code (Twofish cipher makes extensive use of cyclic shift because they are very fast operations)
Cyclic left shift used in some operations in modular arithmetic
How do you multiply 12 x 6 using shifts and addition?
12 x 6 = (12 x 4) + (12 x 2)
What is a tuple?
Ordered sequence of elements stored under a single identifier
Data is immutable
What does immutable mean?
Cannot be changed
What are the uses of a tuple?
Returning multiple items from a function
Passing multiple values to a subroutine but grouped under a single parameter
Often used when returning records from a database
What is used to define a tuple?
()
What is used to access a tuple?
[]
What is an array?
Static data structure
Can only store data of the same data type
What does static mean?
Length doesn’t change during run time
How is an array defined?
[]
What is a 2D array?
Lets you use the equivalent of rows and columns
Each element in an array becomes a separate array
Each coordinate needs two values
How is a 2D array accessed?
name[x,y]
name[x][y]