Binary Computer Coding
Arithmetic Operations
Binary coding involves performing arithmetic operations on binary numbers, just like in decimal arithmetic. Here are some common arithmetic operations in binary:
Binary Addition:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 0 (carry 1)
Binary Subtraction:
- 0 - 0 = 0
- 0 - 1 = 1 (borrow 1)
- 1 - 0 = 1
- 1 - 1 = 0
Binary Multiplication:
- Multiplying two binary digits involves bitwise AND and shifting.
Binary Division:
- Dividing two binary digits involves bitwise division and shifting.
Logical Operations
Logical operations manipulate individual bits in binary numbers:
AND (Logical AND):
- 0 AND 0 = 0
- 0 AND 1 = 0
- 1 AND 0 = 0
- 1 AND 1 = 1
OR (Logical OR):
- 0 OR 0 = 0
- 0 OR 1 = 1
- 1 OR 0 = 1
- 1 OR 1 = 1
NOT (Logical NOT):
- NOT 0 = 1
- NOT 1 = 0
XOR (Exclusive OR):
- 0 XOR 0 = 0
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- 1 XOR 1 = 0
Signed Numbers
In binary, numbers can be represented as signed or unsigned. Signed numbers use the leftmost bit (the most significant bit) as the sign bit:
- Positive Numbers: The leftmost bit is 0. The remaining bits represent the magnitude of the number.
- Negative Numbers (Two's Complement): The leftmost bit is 1. To represent negative numbers using two's complement:
- Invert all bits (change 0 to 1 and vice versa).
- Add 1 to the inverted number.
bit de signe | sequence | resultat |
---|---|---|
0 | 1 1 1 1 1 1 1 | 127 |
0 | 0 0 0 0 0 1 0 | 2 |
0 | 0 0 0 0 0 0 1 | 1 |
0 | 0 0 0 0 0 0 0 | 0 |
1 | 1 1 1 1 1 1 1 | -1 |
1 | 1 1 1 1 1 1 0 | -2 |
1 | 0 0 0 0 0 0 1 | -127 |
1 | 0 0 0 0 0 0 0 | -128 |
For example:
- Positive 3: 00000011
- Negative -3 (8-bit two's complement): 11111101
Keep in mind that these are simplified explanations of binary operations. In real computer systems, there are more complexities and considerations, such as overflow and carry.
Remember that binary coding is fundamental to computer systems and underlies all digital operations and data storage.