0% found this document useful (0 votes)
7 views16 pages

Chapter2 Number Systems and Base Conversion

Chapter 2 explores number systems in computing, focusing on binary, octal, decimal, and hexadecimal systems, their relationships, and conversion techniques. It emphasizes the importance of mastering these conversions for practical programming tasks and provides detailed methods for converting between decimal, binary, octal, and hexadecimal. The chapter includes examples and algorithms for conversions, highlighting the efficiency of hexadecimal representation in computing.

Uploaded by

244g1a04a3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Chapter2 Number Systems and Base Conversion

Chapter 2 explores number systems in computing, focusing on binary, octal, decimal, and hexadecimal systems, their relationships, and conversion techniques. It emphasizes the importance of mastering these conversions for practical programming tasks and provides detailed methods for converting between decimal, binary, octal, and hexadecimal. The chapter includes examples and algorithms for conversions, highlighting the efficiency of hexadecimal representation in computing.

Uploaded by

244g1a04a3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 2 Number Systems and Base

Conversion

Chapter 1 established that a computer stores all information as binary digits — ones and zeros — and
introduced the four number systems that programmers encounter regularly: binary (base 2), octal (base
8), decimal (base 10), and hexadecimal (base 16). This chapter builds directly on that foundation. It
examines each number system in depth, reveals the elegant structural relationships between them, and
develops a complete toolkit of conversion algorithms that allow a programmer to translate any value
between any two bases with confidence and precision.
Mastering number-system conversion is not merely an academic exercise. It is a daily practical skill
in systems programming, embedded development, debugging memory dumps, reading hardware
registers, and working with network protocols. A programmer who is fluent in these conversions
thinks more clearly about what the machine is actually doing with data.

2.1 The Four Number Systems


Every positional number system is defined by two things: its base (also called its radix) and its set of
valid digit symbols. In a positional system, the value of a digit depends not only on the digit itself but
also on the position it occupies within the number. Each position carries a weight that is a power of
the base.
The four number systems used in computing are summarised below:

System Base Valid Digits C Literal Example


(Radix) Prefix
Binary 2 0, 1 0b (C23) 0b1101
Octal 8 0, 1, 2, 3, 4, 5, 6, 7 0 (zero) 0175
Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (none) 123
Hexadecimal 16 0–9 and A–F 0x or 0X 0x7F

Notice that each base is a power of 2. Binary is 2¹, octal is 2³, decimal is not a power of 2 (which is
why it requires the most work to convert to and from binary), and hexadecimal is 2⁴. This power-of-2
relationship between binary, octal, and hexadecimal means conversions between those three systems
can be performed by simple grouping, without any division or multiplication.

2.1.1 Counting in Each System

C Programming: Fundamentals and Practice — Chapter 2


The most direct way to appreciate the difference between number systems is to count from zero upward
in all four simultaneously. The table below does precisely that, beginning at zero and continuing to
fifteen, which is the point at which hexadecimal introduces its first two-digit representation:

Decimal Binary Octal Hexadecimal


0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F

Several observations are immediately apparent from this table and deserve careful attention:
• Binary exhausts its only two digits (0 and 1) after just one count. The next value, 2, requires
an additional digit position, giving 10 in binary. The pattern repeats: a new digit position is
added every time the count reaches the next power of two (2, 4, 8, 16, …).
• Octal counts exactly as decimal does from 0 to 7, then resets. The decimal value 8 becomes
10 in octal (one group of 8, zero units). Octal never uses the digits 8 or 9.
• Decimal counts 0 through 9, then resets. Familiar, but important to recognise as a pattern
rather than as something “natural.”
• Hexadecimal extends the digit alphabet with letters A through F to represent the values 10
through 15. The decimal value 10 becomes the single hex digit A; the value 15 becomes F.
Only at decimal 16 does hex require two digits: 10.

C Programming: Fundamentals and Practice — Chapter 2


The number of binary digits required to represent n is ⌈log₂(n+1)⌉. For the value 7, for example, three
binary digits (111) are needed. For the value 15, four binary digits (1111) are required. This number-
of-digits relationship is explored more formally in the following section.

Key Insight: In any base-B system, a single digit position holds B distinct values (0 through
B−1). The leftmost non-zero digit ‘resets’ to 0 and a new digit is prepended whenever the count
reaches a power of B. This is called a carry.

2.2 Digit Count and Maximum Value


A recurring practical question is: how many digits does a particular number system need to represent
a given value? And conversely, what is the largest value that can be stored in a fixed number of digit
positions?

2.2.1 How Many Digits Are Needed?


The number of digit positions needed to represent the value N in base B is ⌈log₂(N+1) / log₂(B)⌉, which
simplifies to ⌈log_B(N+1)⌉. For the four systems under study, the following practical observations
apply when representing common values:

Value (Decimal) Binary digits needed Octal digits needed Hex digits needed
7 3 (111) 1 (7) 1 (7)
15 4 (1111) 2 (17) 1 (F)
63 6 (111111) 2 (77) 2 (3F)
255 8 (11111111) 3 (377) 2 (FF)
1023 10 (1111111111) 4 (1777) 3 (3FF)

The table makes clear why hexadecimal is valued in computing: it is extremely compact. A value that
requires eight binary digits (one byte) needs only two hexadecimal digits. This makes hex the preferred
notation whenever programmers need to read or write raw binary data.

2.2.2 Maximum Value in n Digit Positions


The maximum value that can be represented using n digit positions in base B is Bⁿ − 1. This is simply
n nines (in decimal), n sevens (in octal), n ones (in binary), or n Fs (in hexadecimal):

Base n=1 n=2 n=3 n=4


Binary (base 2) 1 11 111 1111

C Programming: Fundamentals and Practice — Chapter 2


Octal (base 8) 7 77 777 7777
Decimal (base 10) 9 99 999 9999
Hexadecimal (base 16) F FF FFF FFFF

This pattern has a simple mnemonic: the maximum n-digit value in any base consists of n copies of
the largest single digit in that base (1, 7, 9, or F respectively). Its decimal equivalent is Bⁿ − 1.

Binary: 1111 = 2⁴ − 1 = 15
Octal: 7777 = 8⁴ − 1 = 4095
Decimal: 9999 = 10⁴ − 1 = 9999
Hexadecimal: FFFF = 16⁴ − 1 = 65535

2.3 Converting Decimal to Binary


The most fundamental conversion in computing is from decimal (the system humans use) to binary
(the system the machine uses). Two complementary methods exist: the positional method (used in
Chapter 1 for quick mental conversion) and the repeated-division-by-2 method (a formal algorithm
suitable for any size of number).

2.3.1 Method 1: Positional Subtraction (Power-of-Two Method)


This method was introduced in Chapter 1. The procedure is:
1. Write the powers of two in descending order: … 128, 64, 32, 16, 8, 4, 2, 1.
2. Find the largest power of two that does not exceed the target number. Write a 1 in that
position.
3. Subtract that power from the remaining value. Repeat from step 2 for the remainder.
4. Write a 0 for every power of two that is not used.

This method is fast for small numbers but becomes unwieldy for large ones. The repeated-division
method scales better.

2.3.2 Method 2: Repeated Division by 2


This is the standard algorithmic method for converting any non-negative decimal integer to binary.
The procedure is:
1. Divide the number by 2. Record the quotient and the remainder (which will be 0 or 1).
2. Divide the quotient by 2. Record the new quotient and remainder.
3. Continue until the quotient reaches 0.

C Programming: Fundamentals and Practice — Chapter 2


4. The binary representation is the sequence of remainders read from bottom to top (last
remainder to first).

Example 1: Converting 13 to Binary


Apply repeated division by 2 to the decimal value 13:

13 ÷ 2 = 6 remainder 1 ← least significant bit


6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1 ← most significant bit

Read remainders bottom-to-top: 1 1 0 1

Therefore: 13 (decimal) = 1101 (binary)

Verification using positional weights: 1×8 + 1×4 + 0×2 + 1×1 = 8 + 4 + 0 + 1 = 13. ✓

Example 2: Converting 143 to Binary


Apply repeated division by 2 to the decimal value 143:

143 ÷ 2 = 71 remainder 1 ← LSB


71 ÷ 2 = 35 remainder 1
35 ÷ 2 = 17 remainder 1
17 ÷ 2 = 8 remainder 1
8 ÷ 2 = 4 remainder 0
4 ÷ 2 = 2 remainder 0
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1 ← MSB

Read remainders bottom-to-top: 1 0 0 0 1 1 1 1

Therefore: 143 (decimal) = 10001111 (binary)

Verification: 1×128 + 0×64 + 0×32 + 0×16 + 1×8 + 1×4 + 1×2 + 1×1 = 128 + 8 + 4 + 2 + 1 = 143. ✓

Memory Aid: When performing repeated division by 2, write the remainders vertically
downward as you go. The binary answer is always read upward from the last remainder to the
first. Think of it as a stack: the last item placed in is the first to come out (most significant bit).

C Programming: Fundamentals and Practice — Chapter 2


2.4 Converting Binary to Decimal
Converting from binary back to decimal is the inverse operation. It uses the positional value (weight)
of each bit.

2.4.1 The Positional Expansion Method


The decimal value of a binary number is found by multiplying each bit by its positional weight (the
corresponding power of two) and summing all the products. Only bits that are 1 contribute to the sum;
bits that are 0 contribute nothing.

For a binary number with bits bₙbₙ₋₁…b₁b₀ (where b₀ is the rightmost bit), the decimal value is:

Decimal value = bₙ × 2ⁿ + bₙ₋₁ × 2ⁿ⁻¹ + … + b₁ × 2¹ + b₀ × 2⁰

Example: Converting 1101 to Decimal


The binary number 1101 has four bits. Assign positional weights from right to left:

Bit position 2³ = 8 2² = 4 2¹ = 2 2⁰ = 1
Bit value 1 1 0 1

1 × 8 + 1 × 4 + 0 × 2 + 1 × 1
= 8 + 4 + 0 + 1
= 13

Therefore 1101 (binary) = 13 (decimal).

2.5 Converting Decimal to Octal


Octal uses base 8, so the conversion algorithm mirrors the decimal-to-binary method with the divisor
changed from 2 to 8.

2.5.1 Repeated Division by 8


1. Divide the decimal number by 8. Record the quotient and the remainder (which will be 0–7).
2. Divide the quotient by 8. Record the new quotient and remainder.
3. Continue until the quotient is 0.

C Programming: Fundamentals and Practice — Chapter 2


4. Read the remainders from bottom to top to obtain the octal representation.

Example 1: Converting 65 to Octal

65 ÷ 8 = 8 remainder 1 ← least significant octal digit


8 ÷ 8 = 1 remainder 0
1 ÷ 8 = 0 remainder 1 ← most significant octal digit

Read remainders bottom-to-top: 1 0 1

Therefore: 65 (decimal) = 101 (octal) = 0101 in C

Verification: 1×64 + 0×8 + 1×1 = 64 + 0 + 1 = 65. ✓

Example 2: Converting 123 to Octal

123 ÷ 8 = 15 remainder 3 ← least significant octal digit


15 ÷ 8 = 1 remainder 7
1 ÷ 8 = 0 remainder 1 ← most significant octal digit

Read remainders bottom-to-top: 1 7 3

Therefore: 123 (decimal) = 173 (octal) = 0173 in C

Verification: 1×64 + 7×8 + 3×1 = 64 + 56 + 3 = 123. ✓

2.6 Converting Decimal to Hexadecimal


Hexadecimal uses base 16. The conversion algorithm again mirrors the earlier methods, but with
divisor 16. Because hex requires digits beyond 9, remainders of 10 through 15 must be written as the
letters A through F.

2.6.1 Remainder-to-Hex-Digit Mapping

Remainder 10 11 12 13 14 15
Hex digit A B C D E F

2.6.2 Repeated Division by 16

C Programming: Fundamentals and Practice — Chapter 2


1. Divide the decimal number by 16. Record the quotient and the remainder (0–15, using A–F
for 10–15).
2. Divide the quotient by 16. Record the new quotient and remainder.
3. Continue until the quotient is 0.
4. Read the remainders from bottom to top.

Example 1: Converting 65 to Hexadecimal

65 ÷ 16 = 4 remainder 1 ← least significant hex digit


4 ÷ 16 = 0 remainder 4 ← most significant hex digit

Read remainders bottom-to-top: 4 1

Therefore: 65 (decimal) = 41 (hex) = 0x41 in C

Verification: 4×16 + 1×1 = 64 + 1 = 65. ✓

Example 2: Converting 123 to Hexadecimal

123 ÷ 16 = 7 remainder 11 → 11 = B
7 ÷ 16 = 0 remainder 7

Read remainders bottom-to-top: 7 B

Therefore: 123 (decimal) = 7B (hex) = 0x7B in C

Verification: 7×16 + 11×1 = 112 + 11 = 123. ✓

Example 3: Converting 255 to Hexadecimal

255 ÷ 16 = 15 remainder 15 → 15 = F
15 ÷ 16 = 0 remainder 15 → 15 = F

Read remainders bottom-to-top: F F

Therefore: 255 (decimal) = FF (hex) = 0xFF in C

This result is especially significant: 255 is the maximum value storable in one byte (8 bits = 2⁸ − 1 =
255), and its hexadecimal representation FF fits in exactly two hex digits. This tight correspondence

C Programming: Fundamentals and Practice — Chapter 2


between bytes and hex digit pairs is why hex is the standard notation for byte values throughout
computing.

Practical Rule: One byte always converts to exactly two hexadecimal digits (00 through FF).
This is because 2⁸ = (2⁴)² = 16², so one byte ≡ two hex digits. Padding with a leading zero when
needed (e.g., 0x0A not 0xA) keeps byte representations consistently two characters wide, which
matters enormously when reading memory dumps and network packets.

2.7 Converting Between Binary and Octal


Because 8 = 2³, each octal digit corresponds to exactly three binary digits. This means no arithmetic
is needed: binary and octal convert to each other by simple grouping.

2.7.1 Binary to Octal


1. Starting from the right (least significant bit), group the binary digits into sets of three, padding
with leading zeros on the left if necessary.
2. Replace each three-bit group with its octal equivalent (0–7).

Three-bit group to octal digit lookup:

Binary group 000 001 010 011 100 101 110 111
Octal digit 0 1 2 3 4 5 6 7

Example: Converting 1000011 (binary) to Octal

Binary: 1 000 011


↓ ↓ ↓
Octal: 1 0 3

Pad to three-bit groups from the right:


001 | 000 | 011
1 0 3

Therefore: 1000011 (binary) = 103 (octal)

Verification (decimal): 1×64 + 0×8 + 3×1 = 64 + 0 + 3 = 67. And 1000011 binary = 64+2+1 = 67. ✓

C Programming: Fundamentals and Practice — Chapter 2


2.7.2 Octal to Binary
The reverse is even simpler: replace each octal digit with its three-bit binary equivalent.

Example: Converting 0143 (octal) to Binary

Octal: 0 1 4 3
↓ ↓ ↓ ↓
Binary: 000 001 100 011

Concatenate and drop leading zeros:


1100011

Therefore: 0143 (octal) = 1100011 (binary)

Verification: 1×64 + 1×32 + 0×16 + 0×8 + 0×4 + 1×2 + 1×1 = 64 + 32 + 2 + 1 = 99, and 1×64 + 4×8
+ 3×1 = 64 + 32 + 3 = 99. ✓

2.8 Converting Between Binary and Hexadecimal


Because 16 = 2⁴, each hexadecimal digit corresponds to exactly four binary digits (a nibble). Binary
and hexadecimal therefore convert to each other by grouping into blocks of four, with no arithmetic
required.

2.8.1 Binary to Hexadecimal


1. Starting from the right, group the binary digits into sets of four, padding with leading zeros on
the left if the total number of bits is not a multiple of four.
2. Replace each four-bit group with its hexadecimal equivalent.

Four-bit group to hexadecimal digit lookup:

Binary Hex Decimal


0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4

C Programming: Fundamentals and Practice — Chapter 2


0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15

Example 1: Converting 10001111 (binary) to Hexadecimal

Binary: 1000 | 1111


8 | F

Therefore: 10001111 (binary) = 8F (hex) = 0x8F in C

Verification: 8×16 + 15 = 128 + 15 = 143. And 10001111 binary = 128 + 8 + 4 + 2 + 1 = 143. ✓

Example 2: Converting 101100101 (binary) to Hexadecimal

Pad to multiple of 4 bits from left: 0001 0110 0101

Binary: 0001 | 0110 | 0101


1 | 6 | 5

Therefore: 101100101 (binary) = 165 (hex) = 0x165 in C

2.8.2 Hexadecimal to Binary


Replace each hexadecimal digit with its four-bit binary equivalent. This is the single most useful
conversion a programmer performs in practice.

Example: Converting 0x65 to Binary

C Programming: Fundamentals and Practice — Chapter 2


Hex: 6 5
↓ ↓
Binary: 0110 0101

Concatenate: 01100101

Therefore: 0x65 = 01100101 (binary)

Verification: 64 + 32 + 4 + 1 = 101. And 6×16 + 5 = 96 + 5 = 101. ✓

Example: Converting 0x143 to Binary

Hex: 1 4 3
↓ ↓ ↓
Binary: 0001 0100 0011

Concatenate: 000101000011
Drop leading zeros: 101000011

Therefore: 0x143 = 101000011 (binary)

Verification: 256 + 64 + 2 + 1 = 323. And 1×256 + 4×16 + 3 = 256 + 64 + 3 = 323. ✓

2.9 Converting Between Octal and Hexadecimal via Binary


There is no direct one-step grouping shortcut between octal and hexadecimal, because 8 and 16 are
not powers of each other. The standard approach is to use binary as an intermediate step:

Octal → Binary → Hexadecimal (group by 3, then regroup by 4)


Hexadecimal → Binary → Octal (group by 4, then regroup by 3)

Example: Converting 0123 (octal) to Hexadecimal

Step 1 — Octal to Binary (replace each digit with 3 bits):


Octal: 0 1 2 3
Binary: 000 001 010 011
Combined: 000 001 010 011 = 1010011 (drop leading zeros)

Step 2 — Binary to Hexadecimal (group into 4-bit nibbles from right):


Binary: 0000 | 0101 | 0011

C Programming: Fundamentals and Practice — Chapter 2


Hex: 0 | 5 | 3

Therefore: 0123 (octal) = 0x53 (hex)

Verification: 1×64 + 2×8 + 3 = 64 + 16 + 3 = 83 (decimal). And 5×16 + 3 = 80 + 3 = 83 (decimal). ✓

2.10 The Unified Conversion Summary


The diagram below summarises every conversion pathway between the four number systems. Binary
is placed at the centre because it is the native language of the machine and because all conversions can
route through it.

Conversion Method Effort


Decimal → Binary Repeated division by 2; read remainders bottom-up Moderate
Binary → Decimal Multiply each bit by its power-of-2 weight; sum results Easy
Decimal → Octal Repeated division by 8; read remainders bottom-up Moderate
Octal → Decimal Multiply each digit by its power-of-8 weight; sum results Easy
Decimal → Hex Repeated division by 16; map remainders ≥10 to A–F Moderate
Hex → Decimal Multiply each digit by its power-of-16 weight; sum results Easy
Binary → Octal Group bits in threes from the right; one group = one digit Fast (grouping
only)
Octal → Binary Expand each octal digit to its 3-bit binary equivalent Fast (grouping
only)
Binary → Hex Group bits in fours from the right; one group = one digit Fast (grouping
only)
Hex → Binary Expand each hex digit to its 4-bit binary equivalent Fast (grouping
only)
Octal ↔ Hex Convert via binary as an intermediate step Two-step

The Programmer's Shortcut: Binary ↔ Octal and Binary ↔ Hex conversions require no
arithmetic at all — only grouping and a lookup table that, with practice, becomes memorised. A
professional programmer can convert 0xFF to 11111111 and back almost instantly. This speed
comes from recognising the 16-entry hex-to-nibble table as a small, fixed vocabulary.

2.11 Number System Literals in C

C Programming: Fundamentals and Practice — Chapter 2


C expresses values in all four number systems using a literal prefix notation. When you write a numeric
constant in a C source file, the compiler interprets it according to its prefix:

#include <stdio.h>

int main(void) {
int a = 65; /* decimal: no prefix */
int b = 0101; /* octal: leading zero (= 65) */
int c = 0x41; /* hex: prefix 0x (= 65) */

/* All three variables hold the same value: 65 */


printf("%d %d %d\n", a, b, c); /* prints: 65 65 65 */

/* Printing in different bases using format specifiers */


printf("Decimal: %d\n", a); /* 65 */
printf("Octal: %o\n", a); /* 101 */
printf("Hexadecimal: %x\n", a); /* 41 */
printf("Hex (upper): %X\n", a); /* 41 */

return 0;
}

The printf format specifiers %d, %o, and %x are built-in conversion tools. They allow a single integer
stored in memory to be displayed in decimal, octal, or hexadecimal on demand, without any conversion
code written by the programmer.

2.11.1 The Octal Trap: Accidental Leading Zeros


One of the most common beginner errors in C involves the octal prefix. Because any integer literal
beginning with a zero is interpreted as octal, the innocently written constant 0123 is not one hundred
and twenty-three — it is the octal number 123, which equals 1×64 + 2×8 + 3×1 = 83 in decimal. This
error is particularly insidious when entering telephone numbers, zip codes, or other identifiers that
conventionally begin with zero.

int zip = 07462; /* This is NOT 7462 decimal! */


/* It is octal 7462 = 7*512 + 4*64 + 6*8 + 2 = 3874
decimal */

Warning: Never write an integer literal with a leading zero unless you explicitly intend it to be an
octal constant. For any decimal value, ensure the first digit is non-zero, or store the value as a
string if it is purely an identifier rather than a number.

C Programming: Fundamentals and Practice — Chapter 2


2.12 The Famous Binary Joke
A well-known observation in computing culture states: “There are 10 types of people in the world —
those who understand binary, and those who don’t.” The humour lies in the fact that 10 in binary is
the decimal value 2, so the sentence reads, to a binary-literate reader, as: “there are 2 types of people,”
which is precisely what the sentence then describes. This is a concise illustration of how the same
sequence of digit symbols carries different values depending on which number system is being used
to interpret them. The string 10 means:
• Two — in binary (base 2)
• Eight — in octal (base 8)
• Ten — in decimal (base 10)
• Sixteen — in hexadecimal (base 16)

This ambiguity is exactly why C requires prefixes for non-decimal literals. Without a prefix
convention, the literal 10 could not be interpreted unambiguously by the compiler.
The insight generalises: in any base B, the string “10” always represents the value B itself. This is
because the digits 1 and 0 in a two-digit positional number contribute 1×B¹ + 0×B⁰ = B + 0 = B. The
string “10” is the universal way to write the base, in that base.

2.13 Chapter Summary


This chapter has built a complete, practical toolkit for working with number systems:

• The four number systems used in computing are binary (base 2), octal (base 8), decimal (base
10), and hexadecimal (base 16). Each uses a distinct set of digit symbols and a distinct
positional weight sequence based on powers of its base.
• In a system of base B, n digit positions can represent Bⁿ distinct values (0 through Bⁿ − 1).
The maximum n-digit value consists of n copies of the largest single digit (1, 7, 9, or F).
• Decimal to binary/octal/hex: divide repeatedly by 2, 8, or 16 respectively; read remainders
from last to first.
• Binary/octal/hex to decimal: multiply each digit by its positional weight (power of the base);
sum all products.
• Binary ↔ Octal: group binary digits in threes (three bits = one octal digit).
• Binary ↔ Hex: group binary digits in fours (four bits = one hex digit, called a nibble).
• Octal ↔ Hex: use binary as an intermediate representation.
• In C, decimal literals have no prefix, octal literals begin with 0, and hexadecimal literals
begin with 0x. The printf format specifiers %d, %o, and %x display a value in decimal, octal,
and hex respectively.
• A leading zero on a decimal-intended integer literal in C silently converts it to octal — a
common source of bugs.

C Programming: Fundamentals and Practice — Chapter 2


Review Questions
• Without converting to decimal first, what is the maximum value that can be represented in
four hexadecimal digits? Express your answer in both hexadecimal and decimal.
• Why is binary-to-octal conversion performed by grouping bits in threes rather than some
other number?
• A C program contains the literal value 0144. What decimal value does this represent, and
why might this surprise a programmer who intended to write 144?
• Explain in your own words why hexadecimal is preferred over octal for representing byte
values in modern computing.
• What is the decimal value of the binary number 10001111? Show your working using the
positional expansion method.
• In the C statement int x = 010 + 10; what is the decimal value stored in x? Explain why.

Programming Exercises
• Convert the following decimal numbers to binary using repeated division by 2: 17, 45, 100,
200, 255.
• Convert the following binary numbers to decimal: 10110, 11001100, 10101010, 11111111.
• Convert the decimal values 65, 100, 128, and 255 to both octal and hexadecimal. Verify each
result using the positional expansion method.
• Convert the hexadecimal values 0x1A, 0xFF, 0x7F, and 0xAB to binary directly by nibble
expansion. Then verify each by converting to decimal.
• Write a C program that reads an integer from the user and prints it in decimal, octal, and
hexadecimal using printf format specifiers. Test with the values 65, 123, and 255.
• A byte contains the bit pattern 10001111. Express this value in decimal, octal, and
hexadecimal. How many octal digits and how many hex digits are needed?

C Programming: Fundamentals and Practice — Chapter 2

You might also like