Category: Binary & Logic

  • Two’s Complement: Binary Representation Explained

    Two’s Complement: Binary Representation Explained

    Two’s complement is the standard way to represent signed integers in a fixed number of binary bits. It uses the bit pattern itself to encode positive values, zero, and negative values, allowing the same binary adder to handle signed and unsigned-looking bit patterns.

    This explanation uses an eight-bit word throughout. The two’s-complement binary method connects each pattern to a signed value through positional weights, then uses ordinary binary addition for arithmetic.

    Why does signed binary need two’s complement?

    Bits naturally represent nonnegative values: with eight bits, 00000000 through 11111111 represent 0 through 255 when interpreted as unsigned binary. Signed integers need a way to represent values below zero as well as positive values.

    A signed encoding must also support one representation of zero and practical addition and subtraction. Two’s complement meets these needs without storing a separate sign-and-magnitude field. Every bit contributes to the value, including the most significant bit (MSB), whose weight is negative rather than positive.

    In binary two’s complement, the MSB signals the value range through its weight, but it is not a detachable sign bit attached to an unchanged magnitude. Changing that bit changes the complete numerical interpretation of the pattern.

    How does two’s-complement binary encode values?

    For an eight-bit word, the positional weights are:

    -128, 64, 32, 16, 8, 4, 2, 1

    The leftmost bit has weight -128. Each remaining bit has the familiar positive power-of-two weight. Add the weights of the bits set to 1 to decode the signed value.

    • 00000101 = 4 + 1 = 5
    • 00000000 = 0
    • 01111111 = 127
    • 11111011 = -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5
    • 10000000 = -128

    Positive values have an MSB of 0, while negative values have an MSB of 1. The two’s-complement representation of -5 is therefore 11111011. There is only one zero pattern: 00000000; the system does not need a separate negative zero.

    How does two’s-complement representation support negation and addition?

    To negate a value, invert every bit and add 1, keeping the declared width. This is called invert-and-add-one.

    For example, begin with positive 5:

    00000101 → invert: 11111010 → add 1: 11111011

    Thus, 11111011 represents -5. The process works in reverse as well: inverting 11111011 and adding 1 produces 00000101.

    Once negative values use this encoding, addition remains ordinary binary addition. For example, 5 + (-3) uses:

    00000101 + 11111101 = 1 00000010

    The eight-bit result is 00000010, or 2. The carry beyond the eighth bit is discarded because the word is fixed at eight bits. Subtraction can use the same rule by negating the subtracted value and adding it.

    What range and overflow rules apply to two’s complement in binary?

    An n-bit two’s-complement word represents values from:

    -2n-1 through 2n-1 – 1

    For eight bits, that range is -128 through 127. The range is asymmetric because one bit position has the negative weight -128, while the positive weights add up only to 127.

    Overflow occurs when the exact mathematical result falls outside this fixed-width range. Conceptually, when adding two positive values, a negative-looking result indicates overflow. When adding two negative values, a nonnegative result indicates overflow. Adding operands with different signs cannot overflow.

    For example, 01111111 (127) plus 00000001 (1) produces 10000000, which reads as -128 in eight-bit two’s complement rather than 128. The bit pattern is valid, but the signed result has overflowed. Similarly, -128 plus -1 wraps to the bit pattern for 127, signaling negative overflow.

  • Karnaugh Map: Simplify Boolean Expressions Step by Step

    Karnaugh Map: Simplify Boolean Expressions Step by Step

    A Karnaugh map turns truth-table output values into a layout where adjacent cells represent input combinations that differ in only one variable. By grouping neighboring 1s, you remove variables that change within each group and produce a shorter sum-of-products expression.

    The reliable process is: label the map in Gray-code order, copy each truth-table output into its matching cell, form the largest valid groups, and write one product term for each group.

    How is a Karnaugh map arranged in Gray-code order?

    For three variables, use one variable for the rows and two for the columns. Let A label the rows and BC label the columns:

    • Rows: A = 0 and A = 1
    • Columns: BC = 00, 01, 11, 10

    The column labels use Gray-code order. Each neighboring label changes by one bit, including the transition from the last column, 10, back to the first column, 00. Therefore, the left and right edges are adjacent. The same wraparound rule applies to the top and bottom edges in maps with more row variables.

    Cells that touch along an edge are adjacent. Cells that meet only at a corner are diagonal and are not adjacent. This distinction controls which cells may form a group.

    How do you fill Karnaugh maps from a truth table?

    Start with the truth table’s variable order and match each input combination to its map coordinates. Place a 1 where the function output is 1 and a 0 where it is 0. For example, use this three-variable function with 1s at minterms 0, 1, 2, 4, and 6:

    • BC = 00: row A = 0 has 1; row A = 1 has 1
    • BC = 01: row A = 0 has 1; row A = 1 has 0
    • BC = 11: row A = 0 has 0; row A = 1 has 0
    • BC = 10: row A = 0 has 1; row A = 1 has 1

    The resulting rows are therefore 1 1 0 1 for A = 0 and 1 0 0 1 for A = 1. Keep the labels visible while filling the map; using ordinary binary order such as 00, 01, 10, 11 would incorrectly change the adjacency pattern.

    How does K-map simplification use wraparound, overlap, and power-of-two groups?

    Each group must be a rectangle containing only 1s. Its size must be a power of two: 1, 2, 4, 8, or more cells. Groups may span an edge, and a 1 may belong to more than one group when overlap creates larger or simpler terms.

    In this map, group the two outer columns, 00 and 10, across both rows. They are adjacent through horizontal wraparound, creating a four-cell rectangle. Across this group, A changes and B changes, but C remains 0. This group produces C′.

    The remaining 1 at row A = 0, column BC = 01 can pair with the 1 at column 00 in the same row. That two-cell group overlaps the four-cell group. Within it, A remains 0 and B remains 0, while C changes, so it produces A′B′.

    How do you read the simplified Boolean expression?

    For every group, keep only variables whose values stay constant. Write an uncomplemented variable when it remains 1 and a complemented variable when it remains 0. Omit variables that change inside the group. Then join the retained variables within each term with AND and join the group terms with OR.

    For the example:

    F = C′ + A′B′

    The first term covers all cells where C = 0. The second covers the remaining required 1 where A = 0 and B = 0. This is the result of the Karnaugh-map reduction, with each original 1 covered by at least one valid group.

  • decimal to two’s complement: Convert Signed Values at a Fixed Width

    decimal to two’s complement: Convert Signed Values at a Fixed Width

    To perform decimal to two’s complement conversion, declare the bit width first. For an 8-bit signed value, the range is −128 through +127. Positive values use ordinary binary padded with leading zeros; negative values use the invert-and-add-one method.

    For two’s complement to decimal decoding, inspect the leftmost bit. A 0 means use ordinary positive binary weights. A 1 means the value is negative, so either use a negative sign-bit weight or invert the bits, add one, and negate the result.

    Choose an 8-bit width and represent positive values such as +13

    Bit width is part of the representation because the same visible bits can have different meanings at different widths. For example, 1101 is an unsigned binary value, but an 8-bit signed representation must contain exactly eight bits.

    To represent +13 in 8-bit two’s complement:

    1. Convert the magnitude, 13, to binary: 1101.
    2. Pad on the left with zeros until there are eight bits: 00001101.
    3. Because the first bit is 0, the pattern represents a positive value.

    Check the result by adding the weights of the 1 bits: 8 + 4 + 1 = 13. Thus, the 8-bit pattern 00001101 converts back to +13.

    Convert a negative decimal value using decimal to two’s complement and binary to two’s complement steps

    For a negative decimal value, first write the positive magnitude at the declared width. Then apply the standard binary to two’s complement process: invert every bit and add one.

    Convert −13 to an 8-bit pattern as follows:

    1. Write positive 13 in eight bits: 00001101.
    2. Invert every bit: 11110010.
    3. Add one: 11110011.

    Therefore, −13 is represented as 11110011 in 8-bit two’s complement. The addition is binary addition, so 11110010 + 1 produces 11110011 without changing the width.

    The round-trip check confirms the result. Start with 11110011, invert it to 00001100, add one to get 00001101, and read that magnitude as 13. Since the original pattern had a sign bit of 1, the decoded result is −13.

    Decode two’s complement to decimal using signed bit weights

    For two’s complement to decimal conversion, use the leftmost bit as the sign bit and apply signed weights. In an 8-bit value, the weights from left to right are:

    −128, 64, 32, 16, 8, 4, 2, 1

    This makes the decoding path direct:

    • For 00001101, add the weights under the 1 bits: 8 + 4 + 1 = +13.
    • For 11110011, add the signed weights under the 1 bits: −128 + 64 + 32 + 16 + 2 + 1 = −13.

    The sign bit changes the interpretation rather than simply adding a positive 128. A leading 0 contributes no sign weight, while a leading 1 contributes −128 in an 8-bit representation. This weighted method provides a second check against the invert-and-add-one method.

    Check the 8-bit signed range, overflow, and round-trip results

    An n-bit two’s-complement representation has the range −2n−1 through 2n−1 − 1. With eight bits, that is −27 through 27 − 1, or −128 to +127. The negative side has one extra value because zero uses the positive sign pattern.

    • Valid positive limit: +127 is 01111111.
    • Valid negative limit: −128 is 10000000.
    • Overflow: +128 and −129 cannot be represented as signed 8-bit values.

    Before converting, verify that the decimal value falls within the selected range. After converting, decode the resulting bit pattern back to decimal. The positive round trip is +13 → 00001101 → +13, and the negative round trip is −13 → 11110011 → −13. If the final value differs, check the declared width, padding, bit inversion, and add-one step.