Honors ICS - 2 Coding

Coding Basic

Binary Conversions (decimal fraction) :

image-20260907171416410

Subtraction

Subtraction (for 2 digits) , for decimal system:

63−27=63+(100−27)=63+(99−27)+1=63+¬27+1=63+72+1=136=3663 - 27 = 63 + (100 - 27) = 63 + (99 - 27) + 1 = 63 + \neg 27 + 1=63+72+1=136=36

111111−100101111111-100101 : which is called 1’s complement of 100101 (1补码)

1000000−100101000000-10010 : is called 2’s complement of 100101 (2补码) (Add 1 from 1’s complement code)

So the subtraction become : A3A2A1A0+B3B2B1B0‾+0001A_3A_2A_1A_0+\overline{B_3B_2B_1B_0}+0001

image-20260907173145421

Signed Binary Numbers

Way 1 : let the first bit be the sign bit, then the remaining digit be the magnitude bits, but failed for the addition.

Way 2 : Use the 2’s complement code, and the addition is also the 2’s complement code of the answer.

Conclusion:

  • Positive or 0 : binary code
  • Negative : 2’s complement of AA

So for a 8-bit number : 00000000 - 11111111 :

  • If the first bit is 0, is the positive number or zero

  • If the first bit is 1, then is the 2’s complement code

    Eg : 10000000 is -128, 10000001 is -127, 11111111 is -1

And when 2 numbers are added together with two different signed, it will never be overflow!

Update on 2026.9.15:

In fact, don’t need to distinguish positive and negative numbers distinctly. Just regard the first bit as −128-128 !

Floating Numbersss

Use a scientific notation

image-20260909163826641

The first number don’t need to be consider, as all the binary number will start with 1.

In precision :

v=(−1)S×(1.F)×2E−127v=(-1)^S\times(1.F)\times 2^{E-127}

  • 32-bits : float / single precision
  • 64-bits : double - precision
  • 16-bits : half - precision

To Code Number

Hexadecimal :

image-20260909165831612

Octal Numbers : same as hexadecimal number

BCD : just use 4 bits to show decimal numbers. (the column weights are: 80 40 20 10 8 4 2 1)

Gray Code : Each code between the neighbor, only 1 bit different (This is an unweighted code)

Construct method : by division and flip(Like mirror)

image-20260909170459634

ASCII Code : alphanumeric code

CRC

image-20260909172850613