Книга: Introduction to Microprocessors and Microcontrollers
A quick way to find the two’s complement of a binary number
A quick way to find the two’s complement of a binary number
Start from the left-hand end and invert each bit until you come to the last figure 1. Don’t invert this figure and don’t invert anything after it.
Example 1
What is –2410 expressed as an 8-bit two’s complement binary number?
1 Change the 2410 into binary. This will be 11000.
2 Add leading zeros to make it an 8-bit number. This is now 00011000.
3 Now start inverting each bit, working from the left until we come to the last figure ‘1’. Don’t invert it, and don’t invert the three zeros that follow it.
Example 2
What is –10010 expressed as a 16-bit two’s complement binary number?
1 Convert the 10010 into binary. This gives 11001002.
2 Add nine leading zeros to make the result the 16-bit number 0000000001100100.
3 Now, using the quick method, find the two’s complement:
The result is 1111 1111 1001 1100
Example 3
Find the value of 1011 01112–00 10112 using two’s complement addition.
1 The second number has only six bits so add two zeros on the lefthand end to give 1011 0111–0000 1011.
2 Invert each bit in the number to be subtracted to find the one’s complement. This changes the 00001011 to 11110100.
3 Add 1 to give the two’s complement: 11110100+1=11110101 (or do it the quick way).
4 Add the first number to the two’s complement of the second number:
5 The result so far is 110101100 which includes that extra carry so we cross off the msb to give the final answer of 101011002.
- Subtraction
- Sequence number
- 4.4.4 The Dispatcher
- About the author
- Chapter 7. The state machine
- Appendix E. Other resources and links
- Example NAT machine in theory
- The final stage of our NAT machine
- Compiling the user-land applications
- The conntrack entries
- Untracked connections and the raw table
- Basics of the iptables command