Ch 1. Number Systems

Ch 1. Practice

1.1 Mathematical Data Types

Basic Skills

  1. A weather app displays the current temperature as 21.5°C. What mathematical data type is used to represent this value?
  2. A user enters the letter ‘Z’ into a search bar. What data type is used to store this input?
  3. 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?
  4. A database stores a product as (1023, “Wireless Mouse”, 29.99, true). What kind of data structure is this, and what types are included?
  5. 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

  1. A script processes a folder containing 200 image files. It uses a variable i to track the number of files processed. What data type should be used for i, and why?
  2. 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?
  3. A web application uses a variable isLoggedIn to determine whether a user has successfully logged in. What data type should be used for isLoggedIn, and how is it typically used in conditional logic?
  4. 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.
  5. 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

  1. 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.
  2. 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?
  3. A system uses two Boolean variables: isAdmin and isLoggedIn. 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.
  4. 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.
  5. 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

  1. Convert the binary number 1101012 to its decimal equivalent.
  2. Convert the hexadecimal number 3C16 to decimal.
  3. Convert the decimal number 45 to binary.
  4. Convert the decimal number 255 to hexadecimal.
  5. Convert the binary number 101011112 to hexadecimal.

Applications

  1. A computer system uses hexadecimal to represent memory addresses. A developer sees the address 0x3F in a memory dump. Convert this address to binary and explain how many bits are needed to describe it.
  2. A web designer uses the colour code #FF5733 in a CSS file. Convert each RGB component from hexadecimal to decimal and explain what colour this represents.
  3. 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.
  4. 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.
  5. 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

  1. 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.
  2. A system stores 8-bit values in hexadecimal. If two values C816 and 9F16 are added, the result exceeds 8 bits. Perform the addition in hexadecimal and explain what happens when overflow occurs in an 8-bit system.
  3. A binary number 11110000 is received from a sensor. Convert it to hexadecimal and describe how such patterns might be used in hardware control or device communication.
  4. 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.
  5. 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

  1. Convert the binary number 110111102 to hexadecimal.
  2. Convert the hexadecimal number 4B16 to binary.
  3. Add the hexadecimal numbers 3A16 and 2716.
  4. Perform binary addition: 10112 + 11002.
  5. Subtract 00112 from 10102.

Applications

  1. A software engineer is analyzing a memory dump and sees two hexadecimal values: A216 and 1C16. Add these two values and explain how the result might be used to verify memory integrity.
  2. 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.
  3. 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.
  4. An assembly instruction adds two values: 3E16 and 2916. Perform the addition and explain how hexadecimal arithmetic is used in low-level programming.
  5. A system adds two 8-bit binary numbers: 11110000 and 10101010. Perform the addition and determine whether overflow occurs. Explain how overflow is handled in binary arithmetic.

Challenge Problems

  1. A microcontroller stores 8-bit values. If the values F216 and 1E16 are 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?
  2. Subtract 10011 from 11010 using binary subtraction rules. Show each step of the subtraction and explain how borrowing works in binary.
  3. A network protocol adds two header fields: 7C16 and A916. Perform the addition and explain how hexadecimal arithmetic helps simplify packet header calculations.
  4. Add the binary numbers 1111 and 1111. Show the complete addition process, including all carries, and explain how carry propagation affects performance in digital circuits.
  5. Convert B716 and 3A16 to 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.