Comprehensive Guide to Binary Arithmetic, Two's Complement, and Hexadecimal Systems

Fundamental Computer Logic and Base Number Systems

  • Binary Processing in Computers:

    • Computers process all data exclusively in binary.

    • This occurs because data processing relies on logic gates, which operate with only two distinct states: 00 or 11

  • Base System Definitions:

    • Denary System: Base-1010 number system (using digits 00 through 99

    • Binary System: Base-22 number system (using digits 00 and 11

    • Hexadecimal System: Base-1616 number system (using digits 00 through 99 and letters AA through FF

    • Exactly 44 binary digits (bits) correspond to 11 hexadecimal digit.

  • Padding Binary Numbers:

    • When adding extra digits to a binary number to extend it to a full byte (such as an 88-bit representation), zeros must always be added to the left side of the value, never to the right side.

Binary and Denary Conversions

  • Binary to Denary Conversion:

    • Position each binary digit under its corresponding positional base-22 heading (128,64,32,16,8,4,2,1128, 64, 32, 16, 8, 4, 2, 1

    • Sum the positional values where the bit is set to 11

    • Conversion Example: Convert binary 11101110211101110_2 to denary:

    • 128+64+32+8+4+2=238128 + 64 + 32 + 8 + 4 + 2 = 238

    • Denary value = 238238

  • Denary to Binary Conversion (Successive Division by 2):

    • Divide the denary integer continuously by 22

    • Record the remainder (00 or 11) at each division step.

    • Continue division until the quotient is 00

    • Write out the remainders from bottom to top (last remainder recorded to first remainder recorded).

    • Conversion Example: Convert denary 142142 to binary:

    • 142142 % 2 = 71 \text{ remainder } 0

    • 7171 % 2 = 35 \text{ remainder } 1

    • 3535 % 2 = 17 \text{ remainder } 1

    • 1717 % 2 = 8 \text{ remainder } 1

    • 88 % 2 = 4 \text{ remainder } 0

    • 44 % 2 = 2 \text{ remainder } 0

    • 22 % 2 = 1 \text{ remainder } 0

    • 11 % 2 = 0 \text{ remainder } 1

    • Binary result (reading remainders bottom to top): 10001110210001110_2

Binary Arithmetic, Overflow, and Shifts

  • Binary Addition Rules:

    • 0+0=00 + 0 = 0

    • 0+1=10 + 1 = 1

    • 1+0=11 + 0 = 1

    • 1+1=101 + 1 = 10 (result 00, carry 11

    • 1+1+1=111 + 1 + 1 = 11 (result 11, carry 11

  • Overflow Error:

    • Definition: Occurs when the result of a calculation exceeds the maximum capacity of the allocated number of bits.

    • Limits: In an 88-bit system, the maximum representable denary value is 255255 (11111111211111111_2

    • Cause: An overflow error happens when a calculation produces a result greater than 255255, which cannot be stored within the available 88 bits.

  • Binary Shift Operations:

    • Left Binary Shift:

    • Moves all bits to the left by a specified number of positions.

    • Effect: Multiplies the denary value by 22 for each place shifted left.

    • Example:

      • Starting value: 00010100200010100_2 (16+4=2016 + 4 = 20

      • Shifted 1 place left: 00101000200101000_2 (32+8=4032 + 8 = 40

    • Right Binary Shift:

    • Moves all bits to the right by a specified number of positions.

    • Effect: Divides the denary value by 22 for each place shifted right.

Two's Complement Representation

  • Purpose and Usage:

    • Two's complement is used to represent negative denary numbers in binary.

    • Simplifies the architectural implementation of binary subtraction in computers.

  • Bit Identification:

    • Most Significant Bit (MSB): The leftmost bit in the sequence.

    • Least Significant Bit (LSB): The rightmost bit in the sequence.

  • Positive Denary in Two's Complement:

    • For positive numbers, the Most Significant Bit (MSB) always remains 00

    • Example: Convert positive denary 3838 to an 88-bit Two's complement binary representation:

    • Place values: 128,64,32,16,8,4,2,1128, 64, 32, 16, 8, 4, 2, 1

    • Representation: 00100110200100110_2 (32+4+2=3832 + 4 + 2 = 38, where MSB is 00

  • Converting Positive Denary to Negative Denary in Two's Complement:

    1. Convert the positive denary number to standard binary.

    2. Invert all the bits (flip all 00s to 11s and all 11s to 00s).

    3. Add 11 to the inverted binary result.

Hexadecimal Conversion Methods

  • Hexadecimal Mapping Table:

    • Values 00 through 99 are represented as digits 00 to 99

    • Denary 10=A10 = A

    • Denary 11=B11 = B

    • Denary 12=C12 = C

    • Denary 13=D13 = D

    • Denary 14=E14 = E

    • Denary 15=F15 = F

  • Binary to Hexadecimal Conversion:

    1. Divide the binary string into groups of 44 bits (nibbles) starting from the right.

    2. Convert each 44-bit group into its equivalent hexadecimal character.

    • Conversion Example: Convert 1011111000012101111100001_2 to hexadecimal:

    • Grouping: 1011111000011011 \quad 1110 \quad 0001

    • 10112=1110=B1011_2 = 11_{10} = B

    • 11102=1410=E1110_2 = 14_{10} = E

    • 00012=110=10001_2 = 1_{10} = 1

    • Hexadecimal value = BE116BE1_{16}

  • Hexadecimal to Denary Conversion:

    • Multiply each hexadecimal digit by its corresponding base-1616 positional heading (4096,256,16,14096, 256, 16, 1) and sum the products.

    • Conversion Example: Convert hexadecimal 45A1645A_{16} to denary:

    • Headings: 256,16,1256, 16, 1

    • Hex Digits: 4,5,A4, 5, A (where A=10A = 10

    • Calculation: (4×256)+(5×16)+(10×1)=1024+80+10=1114(4 \times 256) + (5 \times 16) + (10 \times 1) = 1024 + 80 + 10 = 1114

    • Denary value = 11141114

  • Denary to Hexadecimal Conversion (Successive Division by 16):

    • Divide the denary integer continuously by 1616

    • Record the remainder at each step, mapping remainders between 1010 and 1515 to letters AA through FF

    • Read the remainders from bottom to top (last remainder recorded to first remainder recorded).

    • Conversion Example: Convert denary 20042004 to hexadecimal:

    • 2004÷16=125 remainder 42004 \div 16 = 125 \text{ remainder } 4

    • 125÷16=7 remainder 13(13=D)125 \div 16 = 7 \text{ remainder } 13 \quad (13 = D)

    • 7÷16=0 remainder 77 \div 16 = 0 \text{ remainder } 7

    • Hexadecimal value (read bottom to top) = 7D4167D4_{16}

Practical Applications of Hexadecimal

  • Error Codes:

    • Automatically generated by the computer system.

    • Refer directly to the memory location where an error has occurred.

  • Media Access Control (MAC) Addresses:

    • A unique identifier assigned to a network interface controller (NIC) or device on a network.

    • Made up of 4848 bits.

    • Displayed as 66 groups of 22 hexadecimal digits.

    • Rarely changed, enabling persistent device identification regardless of network connection location.

  • IP Addresses (IPv6):

    • The network address assigned to each device connected to a network.

    • IPv6 consists of a 128128-bit number broken down into 1616-bit chunks, represented using hexadecimal format.

  • HTML Color Codes:

    • Used to specify colors of text and elements on computer displays.

    • Intensity levels of different color components are defined by hexadecimal values.