Convert (156.6875)₁₀ to binary, octal and hexadecimal.
How computers represent numbers and characters: positional bases and conversions, complements and signed 2's-complement arithmetic with overflow, BCD, excess-3 and Gray codes, ASCII and Unicode, and IEEE 754 floating point — the arithmetic behind every digital circuit.
A number system writes quantities with a fixed set of symbols called digits. In a positional system the worth of a digit depends on where it stands: the 3 in 300 is worth a hundred times the 3 in 3. The number of distinct digits is the base or radix r, and the digits run from 0 to r − 1.
Every position carries a weight that is a power of the base. Moving left from the radix point the weights are r⁰ = 1, r¹, r², …; to the right of the point they are r⁻¹, r⁻², …. The value of a numeral is simply the sum of digit × weight, and that one rule converts any base to decimal.
| Base r | Name | Digits | Example | Why it is used |
|---|---|---|---|---|
| 2 | Binary | 0, 1 | (1101)₂ = 13 | Two-state hardware: off/on, low/high voltage |
| 8 | Octal | 0–7 | (15)₈ = 13 | Shorthand for binary: 1 octal digit = 3 bits |
| 10 | Decimal | 0–9 | 13 | Human arithmetic, money, keyboards and displays |
| 16 | Hexadecimal | 0–9, A–F (A = 10 … F = 15) | (D)₁₆ = 13 | Shorthand for binary: 1 hex digit = 4 bits (a nibble) |
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 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 |
Computers use binary because their circuits have two reliable states: a transistor is off or on, a voltage is low or high. Octal and hex are not used by the hardware; they are compact ways for people to write long bit strings. A bit is one binary digit, 4 bits make a nibble, 8 bits a byte, and a word is the processor's natural unit (16 bits on the 8086, 32 or 64 on modern CPUs). The leftmost bit is the MSB (most significant bit), the rightmost the LSB.
Converting to decimal is just the weighted sum shown above. Converting from decimal to base r treats the integer part and the fractional part separately, because they shrink in opposite directions: dividing by r peels off digits from the right of the integer, while multiplying by r pushes fraction digits out to the left of the point.
Not every decimal fraction terminates in binary. A fraction ends only if its denominator in lowest terms is a power of 2, so 0.375 = 3/8 ends but 0.1 = 1/10 cannot. Doubling 0.1 gives the carries 0, 0, 0, 1, 1, then 0, 0, 1, 1 again, so (0.1)₁₀ = (0.0 0011 0011 0011 …)₂ recurring. This is why 0.1 + 0.2 is not exactly 0.3 inside a program.
Between binary and octal or hex no arithmetic is needed. Since 8 = 2³ and 16 = 2⁴, each octal digit is exactly 3 bits and each hex digit exactly 4 bits. Group the bits outward from the radix point — leftwards for the integer part, rightwards for the fraction — and pad the outer ends with zeros. Octal ↔ hex goes through binary. The Number-system converter tool on this page shows all the bases side by side so you can check your own conversions.
A complement is the distance from a number to the next power of the base. Digital systems use complements to turn subtraction into addition, so a single adder circuit can both add and subtract. Every base r has two: the r's complement (radix complement) and the (r − 1)'s complement (diminished radix complement).
Hardware has no minus sign, so the leftmost bit is used to encode it. Three conventions are taught: sign-magnitude (sign bit + magnitude), 1's complement (negative = all bits flipped) and 2's complement (negative = flipped + 1). In all three a leading 0 means positive, and positive numbers look identical.
| Decimal | Sign-magnitude | 1's complement | 2's complement |
|---|---|---|---|
| +7 | 0111 | 0111 | 0111 |
| +1 | 0001 | 0001 | 0001 |
| +0 | 0000 | 0000 | 0000 |
| −0 | 1000 | 1111 | (none) |
| −1 | 1001 | 1110 | 1111 |
| −2 | 1010 | 1101 | 1110 |
| −7 | 1111 | 1000 | 1001 |
| −8 | (not representable) | (not representable) | 1000 |
To read a 2's complement number, give the MSB the weight −2ⁿ⁻¹ and the other bits their usual weights: 10110110 = −128 + 32 + 16 + 4 + 2 = −74. Equivalently, if the MSB is 1, take the 2's complement (01001010 = 74) and attach a minus sign. To widen a number, use sign extension — copy the sign bit into the new positions: −5 = 1011 becomes 1111 1011, while +5 = 0101 becomes 0000 0101. Padding a negative number with zeros would change its value (0000 1011 = +11).
| 8-bit operation | Binary addition | Stored result | C | V | Verdict |
|---|---|---|---|---|---|
| (+70) + (+80) | 01000110 + 01010000 | 10010110 = −106 | 0 | 1 | Overflow: +150 > +127 |
| (−5) + (−3) | 11111011 + 11111101 | 1 11111000 → −8 | 1 | 0 | Correct; the carry is discarded |
| (+25) + (−12) | 00011001 + 11110100 | 1 00001101 → +13 | 1 | 0 | Correct; the carry is discarded |
| (−100) + (−50) | 10011100 + 11001110 | 1 01101010 → +106 | 1 | 1 | Overflow: −150 < −128 |
Binary-coded decimal (BCD) encodes each decimal digit separately in 4 bits with weights 8-4-2-1, so it is also called the 8421 code. Only 0000–1001 are used; the six patterns 1010–1111 are invalid in BCD. Thus 586 = 0101 1000 0110 in BCD, whereas the pure binary value of 586 is 1001001010. BCD wastes bits (10 of 16 patterns per nibble) but makes decimal input and display easy — calculators, digital clocks, meters and seven-segment drivers all use it.
Packed BCD stores two digits per byte (the 8086 adjusts it with the DAA and DAS instructions); unpacked BCD stores one digit per byte with the upper nibble 0 (adjusted by AAA and AAS). The ASCII digits '0'–'9' (30H–39H) are unpacked BCD with 0011 in the upper nibble.
Why exactly 6? A 4-bit group naturally wraps at 16, but a decimal digit must wrap at 10. Adding 6 skips the six unused codes 1010–1111, so the group overflows at the right moment and the leftover bits become the correct units digit. Hardware BCD adders detect the need for correction with K = C₄ + S₃S₂ + S₃S₁ (carry out, or a sum of 10–15).
A weighted code gives each bit position a fixed value (8421, 2421, 84−2−1); an unweighted code does not (excess-3, Gray). A code is self-complementing when inverting all four bits of digit d gives the code of 9 − d, i.e. its 9's complement. That property let early decimal machines subtract with simple inverters.
| Digit | 8421 (BCD) | 2421 | 84−2−1 | Excess-3 | Gray (of binary) |
|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 0000 | 0011 | 0000 |
| 1 | 0001 | 0001 | 0111 | 0100 | 0001 |
| 2 | 0010 | 0010 | 0110 | 0101 | 0011 |
| 3 | 0011 | 0011 | 0101 | 0110 | 0010 |
| 4 | 0100 | 0100 | 0100 | 0111 | 0110 |
| 5 | 0101 | 1011 | 1011 | 1000 | 0111 |
| 6 | 0110 | 1100 | 1010 | 1001 | 0101 |
| 7 | 0111 | 1101 | 1001 | 1010 | 0100 |
| 8 | 1000 | 1110 | 1000 | 1011 | 1100 |
| 9 | 1001 | 1111 | 1111 | 1100 | 1101 |
Excess-3 (XS-3) is the 8421 code plus 3 (0011): 5 → 1000, 59 → 1000 1100. It is unweighted yet self-complementing — 2 = 0101 and 7 = 1010 are bitwise complements, and 2 + 7 = 9. It never uses 0000, which helps tell a real zero from a dead line. When two XS-3 digits are added, the raw sum is excess-6, so the rule is: if the digit produced a carry add 0011, otherwise subtract 0011.
Gray code (the reflected binary code, patented by Frank Gray of Bell Labs in 1953) is a unit-distance code: successive values differ in exactly one bit, and it is cyclic (15 → 0 also changes one bit). The n-bit list is built by writing the (n − 1)-bit list, then its mirror image, and prefixing 0s to the first half and 1s to the second. Being unweighted, it is useless for arithmetic, but it prevents false intermediate readings in rotary shaft encoders, orders the rows and columns of K-maps (00, 01, 11, 10), and is used for counters that cross clock domains.
// Binary <-> Gray for non-negative integers
const toGray = (b) => b ^ (b >> 1);
function fromGray(g) {
let b = 0;
for (; g; g >>= 1) b ^= g; // XOR of all right-shifts of g
return b;
}
console.log(toGray(11).toString(2)); // 1110
console.log(fromGray(0b0110)); // 4ASCII (American Standard Code for Information Interchange, first published in 1963 as standard X3.4 by ASA, the body later renamed ANSI) is a 7-bit code with 128 characters: 33 non-printing control codes (0–31 and 127, DEL) and 95 printable characters from 32 (space) to 126 (~). It is usually stored in a byte, with the eighth bit set to 0 or used for parity.
| Character | Decimal | Hex | 7-bit binary |
|---|---|---|---|
| NUL | 0 | 00 | 0000000 |
| LF (line feed) | 10 | 0A | 0001010 |
| CR (carriage return) | 13 | 0D | 0001101 |
| Space | 32 | 20 | 0100000 |
| '0' … '9' | 48 … 57 | 30 … 39 | 0110000 … 0111001 |
| 'A' … 'Z' | 65 … 90 | 41 … 5A | 1000001 … 1011010 |
| 'a' … 'z' | 97 … 122 | 61 … 7A | 1100001 … 1111010 |
| DEL | 127 | 7F | 1111111 |
EBCDIC (Extended Binary Coded Decimal Interchange Code) is IBM's 8-bit code, introduced with the System/360 in 1964 and still used on IBM mainframes. Its letters are not contiguous (there are gaps after I and after R), 'A' = C1H, the digits are F0H–F9H and the space is 40H, so EBCDIC and ASCII data must be translated, not just copied.
Unicode (version 1.0 in 1991) gives one number, a code point, to every character of every script: U+0000 to U+10FFFF, i.e. 17 planes of 65,536 = 1,114,112 code points; plane 0 is the Basic Multilingual Plane. The first 128 code points equal ASCII, and Malayalam sits in U+0D00–U+0D7F. Code points are stored by an encoding: UTF-8 (1–4 bytes, ASCII-compatible, the web's default; Malayalam letters take 3 bytes), UTF-16 (2 or 4 bytes via surrogate pairs; used inside Java and Windows) and UTF-32 (a fixed 4 bytes). India's earlier 8-bit standard for its scripts was ISCII (1991).
Fixed-point numbers cannot cover both an electron's mass and the distance to a star. Floating point stores numbers in binary scientific notation: a sign, a significand (mantissa) and an exponent, value = (−1)ˢ × 1.f × 2ᴱ. The IEEE 754 standard (1985, revised in 2008 and 2019) fixes the formats so every machine gets the same answer.
| Format | Total bits | Sign | Exponent | Fraction | Bias | Approx. normal range | Decimal digits |
|---|---|---|---|---|---|---|---|
| Single (binary32) | 32 | 1 | 8 | 23 | 127 | ±1.18 × 10⁻³⁸ … ±3.4 × 10³⁸ | ≈ 7 |
| Double (binary64) | 64 | 1 | 11 | 52 | 1023 | ±2.2 × 10⁻³⁰⁸ … ±1.8 × 10³⁰⁸ | ≈ 15–16 |
Try the problem first, then reveal the working one step at a time.
Convert (156.6875)₁₀ to binary, octal and hexadecimal.
In 8-bit signed (2's complement) arithmetic, find the sum of −62 and −75 and the states of the carry (C) and overflow (V) flags.
Add 189 and 476 in BCD.
Represent −12.375 in IEEE 754 single-precision format and give the result in hexadecimal.
Convert (3A7.C)₁₆ to decimal and to octal.
Compute 37 − 89 using 8-bit 2's complement arithmetic and interpret the result. Can this operation overflow?
Use the 10's complement method to compute 4590 − 7215.
(a) Convert binary 10110111 to Gray code. (b) Convert Gray code 01101 to binary.
(a) Encode −0.75 in IEEE 754 single precision (hex). (b) Decode C0D00000H. (c) How many bits does decimal 1000 need in unsigned binary and in BCD?
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.