Ch 1. Number Systems
Ch 1. Practice
1.1 Mathematical Data Types
Basic Skills
- A weather app displays the current temperature as 21.5°C. What mathematical data type is used to represent this value?
- A user enters the letter ‘Z’ into a search bar. What data type is used to store this input?
- A smart lock system checks if the door is locked. The result is either true or false. What data type is used to represent the lock status?
- A database stores a product as (1023, “Wireless Mouse”, 29.99, true). What kind of data structure is this, and what types are included?
- A program processes 50 images using a loop counter that starts at 0 and ends at 49. What data type is used for the loop counter?
Applications
- A script processes a folder containing 200 image files. It uses a variable
ito track the number of files processed. What data type should be used fori, and why? - A smart thermostat records the room temperature every minute and stores values like 22.3, 21.8, and 23.0. What data type is appropriate for storing these temperature readings, and why?
- A web application uses a variable
isLoggedInto determine whether a user has successfully logged in. What data type should be used forisLoggedIn, and how is it typically used in conditional logic? - A shopping cart stores each item as a tuple: (ProductID, ProductName, Quantity, PricePerUnit, InStock). Identify the data type of each element in the tuple and explain why each type is appropriate.
- A messaging app stores each message as a sequence of characters typed by the user. What data type is used to store the message, and how is it different from storing a single character?
Challenge Problems
- A smart home system logs the following data for each room: (RoomID, Temperature, MotionDetected, RoomName). For example, (102, 21.7, true, “Living Room”). Identify the data type of each element and explain how the system might use each one in decision-making.
- A developer stores the price of a product as an integer instead of a floating-point number. What potential issues could arise from this decision in an e-commerce application?
- A system uses two Boolean variables:
isAdminandisLoggedIn. Write a logical expression that grants access only if the user is logged in and is an admin. Then explain what happens if either condition is false. - A program stores the character A and the integer 65. Explain how these two values are related in ASCII encoding, and describe a situation where confusing them could cause a bug.
- A developer is deciding whether to use a tuple or an array to store the following data: (UserID, Username, IsActive). Which structure is more appropriate and why? What are the implications for type safety and data access?
1.2 Representations of Numbers in Computers: Binary, Hexadecimal
Basic Skills
- Convert the binary number
1101012to its decimal equivalent. - Convert the hexadecimal number
3C16to decimal. - Convert the decimal number
45to binary. - Convert the decimal number
255to hexadecimal. - Convert the binary number
101011112to hexadecimal.
Applications
- A computer system uses hexadecimal to represent memory addresses. A developer sees the address
0x3Fin a memory dump. Convert this address to binary and explain how many bits are needed to describe it. - A web designer uses the colour code
#FF5733in a CSS file. Convert each RGB component from hexadecimal to decimal and explain what colour this represents. - A digital temperature sensor outputs the binary value
10101100. Convert this binary value to hexadecimal and explain why hexadecimal is preferred for display in embedded systems. - A CPU instruction is stored as the hexadecimal value
B816. Convert this to binary and explain how it might be interpreted at the hardware level. - A network packet contains a header field with the binary value
11001111. Convert this value to hexadecimal and explain how hexadecimal simplifies packet analysis.
Challenge Problems
- A configuration byte is set to
0x3F. Each bit represents whether a feature is enabled (1) or disabled (0). Convert this value to binary and determine how many features are enabled. Explain how bitmasking is used in this context. - A system stores 8-bit values in hexadecimal. If two values
C816and9F16are added, the result exceeds 8 bits. Perform the addition in hexadecimal and explain what happens when overflow occurs in an 8-bit system. - A binary number
11110000is received from a sensor. Convert it to hexadecimal and describe how such patterns might be used in hardware control or device communication. - A designer wants to find a colour halfway between
#FF0000(red) and#00FF00(green). Convert both colours to RGB decimal, compute the midpoint, and convert the result back to hexadecimal. - A file contains the repeating binary pattern
1010 1010 1010 1010. Express this pattern in hexadecimal and explain how recognizing such patterns can help in data compression or optimization.
1.3 Hexadecimal, Binary Conversions and Arithmetic Operations
Basic Skills
- Convert the binary number
110111102to hexadecimal. - Convert the hexadecimal number
4B16to binary. - Add the hexadecimal numbers
3A16and2716. - Perform binary addition:
10112 + 11002. - Subtract
00112from10102.
Applications
- A software engineer is analyzing a memory dump and sees two hexadecimal values:
A216and1C16. Add these two values and explain how the result might be used to verify memory integrity. - A microcontroller register is set to the binary value
11001010. Convert this value to hexadecimal and explain why hexadecimal is preferred when configuring hardware registers. - A network packet contains a header field with the hexadecimal value
7F16. Convert this to binary and explain how individual bits might be interpreted as flags or control bits. - An assembly instruction adds two values:
3E16and2916. Perform the addition and explain how hexadecimal arithmetic is used in low-level programming. - A system adds two 8-bit binary numbers:
11110000₂and10101010₂. Perform the addition and determine whether overflow occurs. Explain how overflow is handled in binary arithmetic.
Challenge Problems
- A microcontroller stores 8-bit values. If the values
F216and1E16are added, the result may exceed the 8-bit limit. Perform the addition and determine whether overflow occurs. What is the final 8-bit result, and what happens to the overflow? - Subtract
10011₂from11010₂using binary subtraction rules. Show each step of the subtraction and explain how borrowing works in binary. - A network protocol adds two header fields:
7C16andA916. Perform the addition and explain how hexadecimal arithmetic helps simplify packet header calculations. - Add the binary numbers
1111₂and1111₂. Show the complete addition process, including all carries, and explain how carry propagation affects performance in digital circuits. - Convert
B716and3A16to binary, add them in binary, and convert the result back to hexadecimal. Show all steps and explain why understanding both binary and hexadecimal is important in low-level programming.