If we have a device that can be in one of two states, we can store a single digit of a base 2, or binary, number on that device. If we have many of these devices, we can store larger numbers, one binary digit per device.
In the picture below, we have a machine with buckets on a conveyor belt. To store a number, the machine fills specific buckets with water. We can consider this machine as representing a base 2 number, where filled buckets represent a 1 and empty buckets represent a 0.
What number is stored on the machine? Answer in base 10.
Whether it’s stored in RAM, in a bank of vacuum tubes, or on a conveyor belt of water buckets, digital data in its most basic form is just a series of individual binary digits, also known as bits. One bit represents one digit in base 2, which is either 0 or 1.
In this course, we’ll adopt the convention used in some programming languages to prefix binary numbers with
0b. What is the base 10 number 173 in binary? Prefix the binary with
How many bits are required to represent 128 in binary?
We can determine the largest base 10 value that can be represented by a certain number of bits in binary by setting all of the bits to 1 and converting to base 10. What’s the largest base 10 number we can represent with 8 bits?
8 bits are referred to as a byte. As we saw in the last problem, the largest number that can be stored in a single byte is 255. Clearly, we require three digits to represent 255 in base 10. What is the smallest base that can represent 255 using only 2 digits?
Base 16 is also referred to as hexadecimal, or hex for short. In this course, we’ll adopt the convention used in some programming languages to prefix hexadecimal numbers with
0x. For example, since 32 is , it will be represented in hexadecimal as
Since one hexadecimal digit needs to represent up to the value 15 in decimal, we will use letters for some of the digits:
The decimal number 47872 is represented as
0xBB00 in hexadecimal. What is the decimal number 48000 in hexadecimal?
As we saw in the previous question, 255 in hexadecimal is
0xFF. This happens to be the maximum possible value that can be represented by 2 digits in hexadecimal. Recall that 255 in binary is
0b11111111, which is also the maximum possible value that can be represented by 8 digits in binary.
This property is important because it means that 2 digits of hexadecimal are exactly equivalent to 8 digits of binary. One implication is that concatenating bytes in either hexadecimal or binary form will give the same result. For example:
We can see that concatenating the hexadecimal forms of 239 and 057 is equivalent to concatenating the binary forms. However, if we concatenate the two decimal values, we get something completely different: 239057.
We normally deal with integers of fixed sizes in multiples of bytes. Recall that a byte is 8 bits. It’s common to see 8-bit, 16-bit, 32-bit, and 64-bit integers in contemporary code. The largest value of a 32-bit integer is which is 4294967295 in decimal. If we store a much smaller number, the remaining unused bits must be set to 0, since sizes are fixed.
What is the decimal value 763 stored in a 32-bit integer, padding the unused bits with
0s? Answer in hexadecimal.