Engineering Reference
Binary, Two's Complement, Gray Code and Bitwise Reference
Lookup for fixed-width binary ranges, two's complement, masks, shifts, carry and overflow, Gray code, Boolean expressions, minterms, and Karnaugh-map terminology.
- Reading Time
- 13 min
- Format
- Representation lookup
- Updated
- September 29, 2026
Fixed-Width Representation
| Representation | Range or formula | Meaning | Boundary |
|---|---|---|---|
| Unsigned n-bit | 0 to 2ⁿ - 1 | All bits contribute non-negative powers of two | Do not interpret the MSB as a sign bit |
| Two's complement n-bit | -2ⁿ⁻¹ to 2ⁿ⁻¹ - 1 | MSB has negative weight in signed interpretation | One more negative value than positive values |
| Sign extension | Replicate signed MSB | Preserves a two's-complement value at greater width | Only valid when the source is intentionally signed |
| Zero extension | Insert leading zeros | Preserves an unsigned value at greater width | Can change a negative signed interpretation |
| Fixed-width wrap | Result modulo 2ⁿ | Keeps the low n bits | Wrapped bit pattern does not mean no overflow occurred |
| Gray code | G = B XOR (B >> 1) | Adjacent sequence positions differ by one bit | Not ordinary binary magnitude and not an error-correcting code |
Common Bit-Width Ranges
| Width | Unsigned | Two's-complement signed | Hex mask |
|---|---|---|---|
| 4 bit | 0 to 15 | -8 to 7 | 0xF |
| 8 bit | 0 to 255 | -128 to 127 | 0xFF |
| 16 bit | 0 to 65,535 | -32,768 to 32,767 | 0xFFFF |
| 32 bit | 0 to 4,294,967,295 | -2,147,483,648 to 2,147,483,647 | 0xFFFFFFFF |
Bitwise Operations and Masks
| Operation | Notation | Effect | Typical use |
|---|---|---|---|
| AND | A & B | 1 only where both bits are 1 | Masking selected fields |
| OR | A | B | 1 where either bit is 1 | Setting selected bits |
| XOR | A ^ B | 1 where bits differ | Toggling and change detection |
| NOT | ~A within n bits | Inverts every bit in the selected width | Must mask unbounded integer representations |
| Left shift | A << k | Moves bits toward higher positions | Fixed-width high bits are discarded |
| Logical right shift | A >>> k | Shifts in zeros | Treats the bit pattern as unsigned |
| Arithmetic right shift | A >> k | Replicates the sign bit | Language behavior and signed width must be explicit |
| Single-bit mask | 1 << position | Selects one zero-based bit | Position must be below bit width |
| Field mask | ((1 << width) - 1) << start | Selects a contiguous field | Avoid shifts at or beyond implementation width |
Canonical Calculation Anchors
| Case | Result | Interpretation |
|---|---|---|
| 8-bit range | 0 to 255 unsigned; -128 to 127 signed | Same width, different interpretation |
| -42 in 8-bit two's complement | 11010110 | Pattern decodes to -42 |
| Binary 1010 to Gray | 1111 | Round-trip returns 1010 |
| Mask bits 0, 2 and 5 | 0b00100101 | Zero-based LSB positions |
| 0xAC AND 0x0F | 0xC | Keeps the low nibble |
| 8-bit NOT 0x0F | 0xF0 | Width mask limits inversion |
| Logical right 0x80 by 1 | 0x40 | Shifts in zero |
| Arithmetic right 0x80 by 1 | 0xC0 | Replicates signed MSB |
Boolean and Karnaugh-Map Terms
| Term | Meaning | Boundary |
|---|---|---|
| Truth table | Enumerates function output for every input combination | n inputs produce 2ⁿ rows |
| Minterm | One asserted input combination in canonical SOP form | Variable order defines its numeric index |
| Don't-care | Input combination permitted to be 0 or 1 during simplification | Use only when the system truly does not constrain the output |
| Prime implicant | Valid power-of-two group not contained in a larger valid group | Wraparound adjacency is allowed |
| Essential prime implicant | Covers at least one minterm no other prime implicant covers | May not complete the entire cover |
| Gray ordering | Adjacent K-map cells differ by one variable | Do not order columns as ordinary binary count |
The project parser confirms A + A·B and A are not equivalent. Its K-map solver reduces minterms 1, 3, 5 and 7 for three variables to C.
Common Errors
- Ignoring bit width.
- Treating a bit pattern as signed and unsigned simultaneously.
- Confusing carry with signed overflow.
- Negating the most-negative value without widening.
- Using zero extension for a negative signed value.
- Applying unbounded NOT without a width mask.
- Confusing arithmetic and logical right shift.
- Using one-based bit positions where the implementation is zero-based.
- Reading Gray code as ordinary binary magnitude.
- Claiming Gray code corrects errors.
- Ordering K-map cells in ordinary binary order.
- Using don't-cares that are reachable system states.
Support reference
FAQ
What range fits in an unsigned n-bit value?
The range is 0 through 2^n - 1. For eight bits that is 0 through 255.
What range fits in an n-bit two's-complement value?
The range is -2^(n-1) through 2^(n-1) - 1. For eight bits that is -128 through 127.
How do I negate a two's-complement value?
Within a fixed width, invert every bit and add one. The most-negative value is a special case because its positive counterpart does not fit at the same width.
Are carry and signed overflow the same?
No. Carry-out is primarily useful for unsigned arithmetic. Signed overflow occurs when the mathematical signed result does not fit the selected two's-complement range.
What is the difference between logical and arithmetic right shift?
Logical right shift inserts zeros. Arithmetic right shift replicates the sign bit of a fixed-width signed value.
Why must bitwise NOT use a width?
Without a width, integer representations may behave as though they have unlimited leading sign bits. A width mask defines which bits are inverted.
How is Gray code calculated?
Binary-reflected Gray code is G = B XOR (B shifted right by one). Conversion back propagates XOR from the most-significant side.
Does Gray code prevent all reading errors?
No. Adjacent ideal code positions differ by one bit, which can reduce ambiguity in transitions, but synchronization, metastability and multi-step errors still require system design.
What is a minterm?
A minterm identifies one input combination for which a Boolean function is asserted, commonly indexed by the binary input pattern.
Why does a Karnaugh map use Gray-code ordering?
Gray ordering makes horizontally or vertically adjacent cells differ by one variable, enabling power-of-two grouping and variable elimination.
Is the Hex Decimal Binary Converter the same as these tools?
No. The converter changes numeral representation. Digital Logic tools additionally apply fixed width, signed interpretation, overflow, masks, shifts and logic minimization.
Connected Engineering Content
Related Resources
Related Calculators
Boolean Logic Calculator
Evaluate Boolean expressions, generate truth tables, compare logical equivalence, validate syntax, and inspect parsed expression structure.
Karnaugh Map Simplifier
Simplify 2, 3, and 4 variable Boolean logic with Karnaugh maps, minterms, don't-care terms, Gray-code ordering, and verified SOP output.
Binary Arithmetic Calculator
Calculate fixed-width binary addition, subtraction, multiplication, division, carry, borrow, overflow, and signed or unsigned interpretation.
Two's Complement Calculator
Convert decimal integers to two's-complement binary, decode signed binary patterns, analyze signed ranges, detect overflow, and perform sign extension.
Binary Bitwise Calculator
Calculate fixed-width binary AND, OR, XOR, NOT, shifts, bit masks, set, clear, toggle, test, register field extraction, and Hamming distance.
Gray Code Converter
Convert binary to Gray code, Gray code to binary, decimal to Gray, and generate Binary-Reflected Gray Code sequences for encoders and digital state analysis.
Related Guides
Related References
Logic Families and Voltage Levels Reference
Condition-aware lookup for VOH, VOL, VIH, VIL, HIGH and LOW noise margins, undefined input regions, family terminology, loading, and mixed-voltage boundaries.
Frequency, Period, dBm and Number-Base Lookup Reference
Derived lookup tables for reciprocal frequency-period values, dBm power anchors and binary, octal, decimal and hexadecimal representations.
