0.1.2 binary Complement

Source: Internet
Author: User

Introduction to programming (Java)-0. 1.2 binary Complement

P6: [Feng? The noriman architecture computer uses binary. The reason why humans are used to decimal, it is said that it is convenient to calculate ratio using fingers, and people have exactly 10 fingers .]

This section describes two sides (page6 and P7. Although binary complement is something that every programmer should be familiar with, it is usually arranged for students to learn by themselves.

Important:Why do I need to use a replacement code?


How can a negative number be expressed in binary when a decimal number is converted to a binary number? For ease of explanation, here we use half a byte (nibble), that is, four bits are the data length.

It is natural to use the highest bit as the symbol bit. Generally, 0 indicates positive and 1 indicates negative. After removing the sign bit, the positive number indicates 0 to the remaining three digits ~ 7, 0b0000 ~ 0b0111 is called the original code. Negative numbers have a simple but problematic representation: the symbol is added with an absolute value, which is seen in the middle column 0-4.


Figure 0? 4. Complement

It has two problems (the following 01 string is not a regular binary representation, so the 0b prefix is not added ):

  • In the entire encoding from 0000 to 1111, two zeros are displayed, namely 0000 and 1000, indicating + 0 and-0. Programming is troublesome when you want to infer whether a number is equal to 0.
  • The more critical problem is that the addition of two positive numbers and the subtraction of two positive numbers (that is, a positive number plus a negative number) do not have a unified algorithm. The following is an example of a problem:

6-3 = 0110 + 1011 = 0001 = 1;
-3-2 = 1011 + 1010 = 0101 (with overflow) = 5

To solve these problemsBinary complement (the two's complement)To encode the value.

Negative Complement Algorithm: Calculate the inverse of each positive number, and then add 1 to the bitwise.

Example:

Positive number: 0b0101

Reverse for each user: 0b1010

Bitwise plus 1: 0b1011

]



Requirements completed:

  • Exercise 0-11: Someone borrowed 500 yuan from programmers and later wanted to borrow 500 yuan. programmers said, simply put together an integer, Which is 524 yuan. Why does Donald Knut give readers 2.56 dollars to find out any of their major errors? (Please note the power of 2)
  • Exercise 0-12: strictly define the binary complement rule. Tip: sign bit, positive complement, and negative complement. (We do not provide binary complement rules. We only introduce the Complement Algorithm of negative numbers. You can do it yourself)
  • Exercise 0-13: The data length is a n-Bit Signed binary integer. Why is the maximum positive number 2n-1-1? What is the maximum negative number? (Java integer value range)
  • Exercise 0-16: Calculate the following questions: (1) 0x9ab4 + 0x5c; (2) 0170-023; (3) 0b1101*0b101

    Exercise 0-17.: Convert to hexadecimal. (1) 0b1001_1100 = 0 ____; (2) 0x7f = 0b _______; (3) 0107 = 0b _________.


  • When studying chapter 1 bitwise operations, the knowledge in this section is very important.
  • Best exercise item: integer arithmetic coding.

0.1.2 binary Complement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.