You really find out bit operation yet? Java, for example in order to summarize

Bit computing is the most close to the real computer arithmetic operation by bit computing, we can efficiently complete a variety of basic operations (addition, subtraction modulo, etc.), we can also use clever bit operation had completed a very complex task, really understand computer, we can better use the computer. I will begin by understanding the basis, to explain some practical applications in Java. Chat in this field, will learn about the content based on the original code •, the inverted, or the like complement or the like and regain • negative shift operand XOR operation details • computing the sign digit

EDITORIAL

Bit computing is the most close to the real computer arithmetic operation by bit computing, we can efficiently complete a variety of basic operations (addition, subtraction modulo, etc.), we can also use clever bit operation had completed a very complex task, really understand computer, we can better use the computer. In this one article, I will start by understanding the basis, to explain some practical applications in Java.

Machine number and the number of machines the true value

A binary number in the computer representation, the number of machines called the number. Machine number is signed, in a computer with the highest number of symbols stored in the machine, a positive number of 0, a negative number. For example, as in the case of 8-bit word length of the machine (machine word is the number of bits of binary data directly to the computer processing, which determines the accuracy of calculation of a computer, typically an integer multiple of 8, 8, 16 bit, 32-bit, 64-bit, 128-bit), decimal +3 is converted to binary 0000 0011, if -3 is converted into binary 10000011. Binary conversion of 00,000,011 and 10,000,011 is the number of machines.

Here we also need to know the true value is the number of machines, due to the first digit of the machine is the sign bit, so the number of machines in the form of value is not equal to the true value. The above machines have a number of symbols, for example, the number 10000011, which represents the most significant bit negative, its real value is -3, instead of 131 as a value (equal to 10,000,011 to dec 131), so that, for the sake of distinction, the signed, It becomes a real value corresponding to the true value of the machine. Such true value = 0000 0001 0001 + 000 = 0001 +1,1000 true value = -1 = -0000001

Basic concepts and method of original codes, and anti-complement code

Above we understand the number of machines, which is a binary number, but the computer to be stored using a certain coding method, the original code, anti-code and complement code is stored in a machine-specific digital encoding.

Original code

Original code is the sign bit plus the absolute value of the true value, i.e. with a first symbol representing the remaining bits represent a value, such as: if the 8-bit binary:

[+1] = 0000 0001 Original

[-1] = 1000 0001 Original

The first one is the sign bit as the first bit is the sign bit, so the range is 8-bit binary :( i.e. a value not indicating the first, only said positive and negative.) [11111111, 01111111], and decimal is [-127, 127] (beep quietly, in fact, may be said to be the number of the original machine code signed).

Inverted

Positive anti-code is its own, the inverted negative which is the basis of the original code, change the sign bit, each bit is inverted to rest.

[L] = [0000 0001] Original = [0000 0001] trans

[-1] = [1000 0001] Original = [1111 1110] trans

Complement

Complement representation is a positive number that complement its own, negative complement is based on its original code, the sign bit unchanged, the rest of you negated, finally +1 (that is, its anti-code Based on the +1)

[L] = [0000 0001] Original = [0000 0001] trans = [0000 0001] Complement

[-1] = [1000 0001] Original = [1111 1110] trans = [1111 1111] Complement

Once you know the basic concepts of these three, it is worth mentioning that, if the addition with inverted, causes problems (-0 +0) two zeros, so we use complement, not just to repair the symbols 0 and the presence of two coding problems, but also to represent more than a minimum number. This is why the 8-bit binary, using the original code or codes represented by anti-range [-127, +127], was used to complement representation range [-128, 127]. Because the machine's complement, so the 32-bit int type commonly used in programming the signed may represent are: [-231, 231-1] as the sign bit is represented by a first, complement representation when used You can save more than a minimum.

Operators in Java

Note that, all the bits of the following operations are performed by the complement, the complement number is positive in itself, a negative count corresponding to itself, both operands are positive, the result Binary Decimal direct access, if two wherein the operand is a negative number or both negative, the sign bit if the result is 1 (i.e., negative), the complement is obtained, the need to complement the original code, converted to a decimal, the result if sign bit is 0, direct access to decimal to binary transfer. That is complemented operation comes just as negative, the result is to take every negative original. You can put yourself below convert decimal numbers into binary complement of all, and then take into count, to look at is not the correct result.

Exclusive OR operation is symbol "^", the same is 0, 1 is different, for example the following codes:

public static void main(String[] args) {    System.out.println("2^3 运算的结果是 :"+(2^3));    //打印的结果是:   2^3 运算的结果是 :1}//2 的二进制 0010,3 的二进制 0011,2^3 就为 0001,结果就是 1public static void main(String[] args) {    System.out.println("-2^3 运算的结果是 :"+(-2^3));    //打印的结果是:   -2^3 运算的结果是 :-3}//-2 的二进制补码 1110,3 的二进制 0011,-2^3 就为 0001,结果就是-3

It is the arithmetic sign "&", as long as there is a 0 on 0, the code example as follows:

public static void main(String[] args) {     System.out.println("2&3 运算的结果是 :"+(2&3));     //打印的结果是:   2&3 运算的结果是 :2}public static void main(String[] args) {     System.out.println("-2&3 运算的结果是 :"+(-2&3));     //打印的结果是:   -2&3 运算的结果是 :2}

Is OR operator "|", as long as there is a 1 to 1, the code example as follows:

public static void main(String[] args){    System.out.println("2|3 运算的结果是 :"+(2|3));    //打印的结果是: 2|3 运算的结果是 : 3}public static void main(String[] args){    System.out.println("-2|3 运算的结果是 :"+(-2|3));    //打印的结果是: -2|3 运算的结果是 : -1}

Non-operator is "~" is negated you, for example the following codes:

public static void main(String[] args){    System.out.println("~5 运算的结果是 :"+(~5));    //打印的结果是: ~5 运算的结果是 : -6}public static void main(String[] args){    System.out.println("~(-5)运算的结果是 :"+(~(-5)));    //打印的结果是: ~(-5)运算的结果是 : 4}

Displaced leftward symbol "<< binary n-bit shifted to the left, back filled with zeros

public static void main(String[] args) {     System.out.println("2<<3 运算的结果是 :"+(2<<3));     //打印的结果是:   2<<3 运算的意思是,向左移动 3 位,其结果是 :16}public static void main(String[] args) {     System.out.println("-2<<3 运算的结果是 :"+(-2<<3));     //打印的结果是:   -2<<3 运算的意思是,向左移动 3 位,其结果是 :-16}

Displaced rightward symbol ">>", a binary right shift of n bits, if the value is positive, 0 is inserted at a high level, if the value is negative, 1 is inserted in the high

public static void main(String[] args) {     System.out.println("2>>3 运算的结果是 :"+(2>>3));     //打印的结果是:   2>>3 运算的意思是,向右移动 3 位,其结果是 :0}public static void main(String[] args) {     System.out.println("-2>>3 运算的结果是 :"+(-2>>3));     //打印的结果是:   -2>>3 运算的意思是,向右移动 3 位,其结果是 :-1}

Unsigned right shift symbol ">>>", ignoring the sign bit, 0 vacancies are filled. ">>>" and ">>" The only difference is that it no matter what the original leftmost number, all are filled with zeros. For example, 8-bit byte, a type byte -1 expressed as 11111111 (complement notation) b >>> 4 unsigned right shift is 4, i.e., 00001111, this result is 15. (Quietly beep, do not ask me if I had left unsigned, and so then you really mastery, you will find this is a very low problem.)

public static void main(String[] args) {     System.out.println("16>>2 运算的结果是 :"+((16)>>2));     //打印的结果是:   16>>2 运算的结果是 :4}public static void main(String[] args) {     System.out.println("-16>>2 运算的结果是 :"+((-16)>>2));     //打印的结果是:   -16>>2 运算的结果是 :-4}public static void main(String[] args) {     System.out.println("16>>>2 运算的结果是 :"+((16)>>>2));     //打印的结果是:   16>>>2 运算的结果是 :4}public static void main(String[] args) {     System.out.println("-16>>>2 运算的结果是 :"+((-16)>>>2));     //打印的结果是:   -16>>>2 运算的结果是 :1073741820}

Do not count multiplication and division, multiplication and division

addition

+ 9 to 13, for example, we like to split this calculation process:

  • Step a: not considering the carry bit, respectively, of each digit are summed result is stored as sum, plus the digit 9 3 2; 1 plus ten digits 0 to 1; the final result is 12;
  • Step two: only considering the carry bit, the result is stored as a carry, 3 + 9 there is a carry, the carry is 10;
  • Step three: if the result of step two resulting binary carry is not 0, and the sum obtained in step a two step resulting carry, repeat steps one, two, three. If the end of the carry is 0, the final result is obtained in step a sum.

In fact, this is to sum = 12 and carry = 10 repeat the above three steps

  • (A) without considering the carry bit, respectively, for each digit of the sum, sum = 22;
  • (B) only considering the carry bit: step does not carry on, the carry = 0; (c) step 2carry = 0, the end result is the sum = 22.

This is a demonstration of our operations in the decimal, binary and see that we are not replaced, a binary 13 is binary 0000 1101,9 0000: 1001

  • Step: not considering the carry bit, respectively, for each digit of the sum, sum = 0000 1101 + 0000 1001 = 0000 0100
  • Step Two: considering the carry bit, there are two binary bit 0 and bit 3, only considering the carry bit of the result is: carry = 0001 0010
  • The third step: carry == 0, not zero, repeat step one, two, three; 0 ends, the result is the sum?.

In the present embodiment

  • Without considering the carry bit sum = 0001 0110;
  • Only considering the carry bit carry = 0;
  • carry == 0, the end result is the sum = 0001 0110

22 is just converted to decimal. In fact, three steps, pseudo-code image to be understood as follows, for example with the 3 + 9.

a = 0011, b = 1001;    start;    first loop;    1.1 sum = 1010    1.2 carry = 0010    1.3 carry != 0 , go on;    second loop;    2.1 sum = 1000;    2.2 carry = 0100;    2.3 carry != 0, go on;    third loop;    3.1 sum = 1100;    3.2 carry = 0000;    3.3 carry == 0, stop; result = sum;end
//1、递归形式实现int add(int a ,int b){    if (b == 0)        return a;    else{        int carry = (a & b) << 1;        a = a ^b;        return add(a,carry);    }}//非递归形式实现int add2(int a ,int b){   int carry;   while (b != 0){   carry = (a & b) << 1;       a = a ^b;       b = carry;   }   return a;}

Subtraction

We know the bit operations to achieve the addition operation, subtraction that is relatively simple and a little more. We achieve the addition operation, naturally, we will think of the subtraction 11--6 deformed adder 11 + (-6), i.e. a positive number plus a negative. Yes, very clever, in fact, our computer operations, too, that some people will say why the computer does not achieve the same, like the adder a subtracter it? Right, so it is reasonable to assume, but considering the subtraction than addition to the complex, more difficult to achieve. why? We know that there are only adding two operations, plus, carry, and do subtraction, subtraction operation will take place, if the current location is not enough to do the subtraction subtract it from the high borrow, there will be a problem, borrow express how it? Addition operation, carry and left one achieved by the operation, and borrow really bad shows. So we naturally think of to turn subtraction into addition. How to achieve it?

We just said subtraction can be converted into a positive number plus a negative number, that we must first look at how negative numbers in a computer is represented.

+8 in the computer that is 1000 in binary, that represents -8 how it? It is easy to think of, can be a binary digit (bit) special provisions for the sign bit, which is equal to 0 to indicate a positive number, equal to 1 for a negative number. For example, 8-bit machine, a predetermined highest bit is the sign bit of each byte. So, +8 is 00001000, and -8 is 10001000. It's just intuitive representation, in fact, the computer is by 2's complement to represent negative, what is 2's complement (the same complement, English is the 2's complement, in fact, should be translated as two's complement of) it? It is a binary representation of signed number, the embodiment is also a sign of the number of the variable number, the step of obtaining:

  • The first step, each bit have taken the opposite value, becomes 0 becomes 0 1,1 (i.e., inverted).
  • A second step, a value obtained in the previous step (inverted) plus 1.

Is simply negated plus one!

int subtraction(int a ,int b){     b = ~b+1;     return this.add(a,b); }

multiplication

We consider the process of seeking real life manual product, this approach also applies to the binary, here I am at 13 * 14, for example, we demonstrate how to find the absolute value of the multiplier and multiplicand multiplied by manually calculated. DiagramAs can be seen from the graph calculation process, if the current multiplier bit is 1, then take the results of the multiplicand is added to a final result the left; if the current bit is 0, 0 is added to the product to take in (plus 0 is, do nothing)

  • Determining whether the multiplier factor is 0, skip to step 0 (4)
  • And a multiplier for calculating and determining the end bit is 0 or 1, if 1, then the sum of the number of the current multiplicand; if 0, the sum is 0; adding to the final number results;
  • Multiplicand left one, right one multiplier; back to step (1)
  • Determining the sign bit output;
      //a 被乘数,b 乘数      int multiplication(int a,int b){          int i = 0;          int res = 0;          //乘数不为 0          while (b != 0){              //处理当前位              //当前位是 1             if ((b & 1) == 1){                 res += (a << i);                 b = b >> 1;                 //记录当前是第几位                 i++;             }else {                 //当前位是 0                 b = b >> 1;                 i++;             }         }         return res;     }

division

Division occur easily can be converted into a subtraction, that is kept by subtracting the dividend the divisor until the divisor is less than the divisor, the frequency is reduced at this time we need quotient, the dividend is a time when the remainder. It should be noted that the determination of the sign, symbols and commercially multiplication is determined as the sign of the product, i.e., depending on the dividend and divisor, with the card number, the negative number is different; the same remainder and dividend sign. Is a binary computer world, all the data can be used an int [2 ^ 0, 2 ^ 1, ..., 2 ^ 31] to represent a group of group (int-Max 31). Difficult to think of a divisor 31,2 ^ 30 ^ 2, ..., 2 ^ 0 ^ 2,2 ^ 1,2 times to try to reduce the dividend, if moving down so, put in respective multiple providers added; if Save does not move, then try to turn a smaller multiples. So you can quickly approaching the end result.

2 to the power of fact, i left i-bit equivalent, from 31 Why start it? Because the maximum value of data int type is 2 ^ 31 ah.

    int division(int a,int b){          int res;          if(a<b){              return 0;          }else{              res=division(subtraction(a, b), b)+1;          }          return res;     }

Read more: http://gitbook.cn/gitchat/activity/5e46135165ec7013893ec3ba

You can also download CSDN community's quality original content GitChat App, read more GitChat exclusive technical content Oh.

FtooAtPSkEJwnW-9xkCLqSTRpBKX

Released 3634 original articles · won praise 3487 · Views 3.25 million +

Guess you like

Origin blog.csdn.net/valada/article/details/104321752