User Tools

Site Tools


tanszek:oktatas:techcomm:encoding_integers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tanszek:oktatas:techcomm:encoding_integers [2024/09/30 18:17] – created kneheztanszek:oktatas:techcomm:encoding_integers [2024/09/30 18:54] (current) – [How to convert decimal integers to binary form] knehez
Line 38: Line 38:
   * Invert the bits: **11111010**   * Invert the bits: **11111010**
   * Add one: **11111011**   * Add one: **11111011**
 +
 +**Example**: Add -5 + 7 in 8 bits binary format
 +
 +Now we can add the two binary numbers:
 +
 +<code>
 +  11111011   (-5 in two's complement)
 ++ 00000111   (7)
 +------------
 +  00000010   (2)
 +</code>
 +
 +This is the reason why we are using the special two's complement form. Without using this encoding, we cannot add negative and positive numbers.
 +
 +==== How to convert decimal integers to binary form ====
 +
 +Let’s convert the decimal number **156** into binary.
 +
 +==== Step 1: Divide the decimal number by 2 ====
 +Start by dividing the decimal number (156) by 2 and keep track of the quotient and remainder. The remainder will be either 0 or 1, which forms the binary digits from bottom to top.
 +
 +| **Division** | **Quotient** | **Remainder** |
 +| 156 ÷ 2          | 78       | 0         |
 +| 78 ÷ 2           | 39       | 0         |
 +| 39 ÷ 2           | 19       | 1         |
 +| 19 ÷ 2           | 9        | 1         |
 +| 9 ÷ 2            | 4        | 1         |
 +| 4 ÷ 2            | 2        | 0         |
 +| 2 ÷ 2            | 1        | 0         |
 +| 1 ÷ 2            | 0        | 1         |
 +
 +==== Step 2: Write the binary digits ====
 +To get the binary representation, take the remainders from bottom to top. The binary equivalent of **156** is:
 +
 +**10011100₂**
 +
 +==== Verification ====
 +To verify, we can convert the binary number back to decimal:
 +
 +  (1 × 2⁷) + (0 × 2⁶) + (0 × 2⁵) + (1 × 2⁴) + (1 × 2³) + (1 × 2²) + (0 × 2¹) + (0 × 2⁰)
 +  = 128 + 0 + 0 + 16 + 8 + 4 + 0 + 0
 +  = 156
 +
 +Hence, the binary representation of 156 is correct.
 +
 +
tanszek/oktatas/techcomm/encoding_integers.1727720249.txt.gz · Last modified: 2024/09/30 18:17 by knehez