tanszek:oktatas:techcomm:encoding_integers
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tanszek:oktatas:techcomm:encoding_integers [2024/09/30 18:17] – created knehez | tanszek: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**: | ||
+ | |||
+ | Now we can add the two binary numbers: | ||
+ | |||
+ | < | ||
+ | 11111011 | ||
+ | + 00000111 | ||
+ | ------------ | ||
+ | 00000010 | ||
+ | </ | ||
+ | |||
+ | 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, | ||
+ | |||
+ | **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