Writing in the front

All the data in a computer is stored in binary form on the device. In the two states of 0 and 1, the computing (+, -, *, /) carried out by the computer on binary data are called bit operations, that is, the operation in which the sign bits participate in the operation together.

On the base

Base is a representation of data in a computer. Numbers in the n-base can be represented by numbers ranging from 0 to (n-1), and those larger than 9 are represented by letters A-F.

  • Decimal: The number ranges from 0 to 9 and carries 1 into every 10
  • Binary: Consists of 0 and 1
  • Octal: Consists of 0 to 7, carrying 1 on every 8
  • Hexadecimal: The value consists of 0 to 9 and A to F. 0-9 corresponds to 0-9, and A-F corresponds to 10-15. Letters are case insensitive

Base conversion

Binary to decimal

The weight of the 0th bit of a binary number is 2 to the 0 power, and the first bit is 2 to the 1 power……

So, given a binary number: 101100100, convert to decimal (from right to left) :

0x2 0 + 0x2 1 + 1×2 2 + 0x2 3 + 0x2 4 + 1×2 5 + 1×2 6 + 0x2 7 + 1×2 8 = 356

Octal to decimal

The 0th bit of an octal number has a weight of 8 to the 0th power, and the first bit is 8 to the 1st power……

So, take an octal number: 1507 and convert it to decimal (from right to left) :

7×8 0 + 0x8 1 + 5×8 2 + 1×8 3 = 839

Convert hexadecimal to decimal

The weight of the 0th bit of a hexadecimal number is 16 to the 0, and the first bit is 16 to the 1……

So, take a hexadecimal number 2AF5 and convert it to decimal (from right to left) :

5×16 0 + Fx16 1 + Ax16 2 + 2×16 3 = 10997

Fast 2 -,10 -, hexadecimal conversion

Binary 8421

Let’s start with a binary number: 1111. What is it? You might also want to calculate this: 1×2º+1×2¹+1×2²+1×2³=1×1+1×2+1×4+1×8=15.

We have to memorize the weights of each bit of 1111 directly, and write them down from top to bottom: 8, 4, 2, 1.

That is, the weight of the highest bit is 2³=8, followed by 2² = 4,2 ¹=2, 2º=1.

Remember 8, 4, 2, 1, for any 4 bit binary number, we can quickly figure out its decimal value.

Next, let’s practice using 8421 method for fast calculation, 2,10 hexadecimal conversion

1111 = 8 + 4 + 2 + 1 = 15 =F 1110 = 8 + 4 + 2 + 0 = 14= E 1101 = 8 + 4 + 0 + 1 = 13= D 1100 = 8 + 4 + 0 + 0 = 12 =C 1011 1010 = 8 + 0 + 2 + 0 = 10 =A 1001 = 8 + 0 + 0 + 1= 9 =9 0001 = 0 + 0 + 0 + 1= 1= 1 0000 = 0 + 0 + 0= 0= 0Copy the code
  • Binary to hexadecimal

    Convert to hexadecimal in 4-bit segments, for example:

  • Convert hexadecimal to binary

    Conversely, when we see a FD, how do we quickly convert this hexadecimal to binary?

    When we see F, we need to know that it’s 15, and then how do we add 15 to 8421?

    It should be: 8 + 4 + 2 + 1, so all four digits are 1:1111.

    And then I convert D, and I see D, and I know it’s 13, and how do I make 8421 out of 13?

    It should be 8 + 4 + 1, that is, 1101.

    Therefore, FD is converted to binary number, which is 1111 1101

  • Decimal to binary

    Since the conversion from hexadecimal to binary is fairly straightforward, when we need to convert a decimal number to binary, we can also convert to hexadecimal and then to binary. For example, if the decimal number 1234 is converted into a binary number, it takes a lot of computations to divide by 2 all the time to get the binary number directly. So we can divide by 16 to get a hexadecimal number:

    1234 present 16 = 77... 2 77 present 16 = 4... 13 (D) 4 present 16 = 0... 4Copy the code

    The hexadecimal value is 4D2

    To binary corresponds to:

    0100   4
    1101   D
    0010   2
    Copy the code

    That is, the conversion from decimal 1234 to binary is 0100 1101 0010

  • Binary to decimal

    Similarly, if a binary number is very long and we need to convert it to decimal, we can convert the binary to hexadecimal and then to adecimal, in addition to the previous method. For example, a binary of type int looks like this:

    0110 1101 1110 0101 1010 1111 0001 1011
    Copy the code

    We convert to hexadecimal by four digits: 6 D E 5 A F 1 B

    Conversion from hexadecimal to decimal:

    Bx16 0 + 1×16 1 + Fx16 2 + Ax16 3 + 5×16 4 + Ex16 5 + Dx16 6 + 6×16 7 = 1843769115

  • Convert decimal to hexadecimal

    Remainder theorem decomposition, such as 487710 into hexadecimal:

    487710 present 16 = 30481... 14 (E) 30481 present 16 = 1905... 1 1905 present 16 = 119... 1 119 present 16 = 7... 7 7 present 16 = 0... 7Copy the code

    487710(10) = 7711E(16)

An operation

Calculations in computers are carried out in binary, so reasonable use of bitwise operators can improve the efficiency of code execution compared with direct use of (+, -, *, /) operators in code.

symbol describe algorithm
& with If both bits are 1, the result is 1(1&1=1, 0&0=0, 1&0=0).
| or Two bits are zero, the result is 0 (1 1 = 1, 0 | | 0 = 0, 1 | 0 = 1)
~ Bit non (invert) ~1=0, ~0=1
^ An exclusive or 1 ^ 1=0, 1 ^ 0=1, 0 ^ 0=0.
>> There’s a sign to the right If positive, high complement 0, negative, high complement 1
<< There’s a sign to the left If positive, high complement 0, negative, high complement 1
>>> Unsigned right shift No matter positive or negative, the high is added 0

And (&)

4 & 6 = 4

First we need to convert two decimal numbers to binary

4 0 1 0 0 6 0 1 1 0 ---------- 4 0 1 0 0 0 0 0Copy the code

use

  • Clear: If you want to clear a unit, as long as it is matched with a value whose bits are all zeros, the result is zero

  • Parity: If it’s 0 or 1 at the end, it’s even. If it’s 1, it’s odd

    If ((a & 1) == 0) // if (a % 2 == 0)Copy the code

Or (|)

4 & 6 = 6

First we need to convert two decimal numbers to binary

4  0 1 0 0
6  0 1 1 0
----------
6  0 1 1 0
Copy the code

use

  • Commonly used to some bits of a data set to 1: if a number is to be low four of X = 10101100 is set to 1, then make Y = 00001111, as a result of X and Y are or will be X low four set to 1 (X | Y = 10101111)

Not (~)

In Java, all data representation method is in the form of complement representation, if not specified, the default data type in Java is int,int data type is 4 bytes, 1 byte is 8 bits, so int is 32 bits, 32bit.

The relation between complement and original code: the positive complement is the same as the original code, and the negative complement is the inverse of the original code minus 1

For example, 5. The source code is 00000000 00000000 00000000 00000101. The complement code is 00000000 00000000 00000000 00000101 (computer storage). 10000000 00000000 00000101 Complement: 11111111 11111111 11111111 11111111 (computer internal storage)Copy the code

Note: In binary, the highest bit is the sign bit 1 for negative and 0 for positive

Now that we know the source complement let’s look at the bit non-~ operator

Example: ~ 4

4  0 1 0 0
----------
-5  1 0 1 1
Copy the code

To convert 4 to binary, the value is 0100

The complement code is 00000000 00000000 00000000 00000100

Take the reverse: 11111111 11111111 11111111 11111111 11111111 11111011(this is the complement of ~4 stored in the computer)

You need to compute the complement out of the original code and then convert it to the decimal system, keeping the high order the same, plus 1

10000000 00000000 00000000 00000101

To the decimal system, the value is -5

So minus 4 is minus 5

Xor (^)

4 to the sixth is 6

First we need to convert two decimal numbers to binary

4  0 1 0 0
6  0 1 1 0
----------
2  0 0 1 0
Copy the code

The same is 0, the different is 1

use

  • Swap two numbers: one number is the same number as the other number by swapping or swapping them twice

    Void swap (int a, int b) {if (a! = b) { a = a ^ b; b = a ^ b; A = a ^ b; //a = a ^ b; A ^b^a}} a^b^a}}Copy the code

Moves to the right (> >)

4 > = 2 > 1

To convert 4 to binary, the value is 0100

Positive, high complement 0, negative, high complement 1

Shift to the left (< <)

4 < < 1 = 8

To convert 4 to binary, the value is 0100

Positive, high complement 0, negative, high complement 1

Negative numbers are explained in the non-operation and are not shown here.

Unsigned right shift (>>>)

Unsigned right shift (>>>) is only meaningful for 32-bit and 64-bit.

When we move bits, we do the same thing as the right shift operator, except when we complement bits, whether it’s a zero or a one, we complement zeros.

Interesting modulo properties

A % (2^n) is equivalent to a & (2^n – 1), so the number of arrays in the map must be a power of 2.

Public static void main(String[] args) {// system.out.println ("the 355% 16 is: "+ (345% 16)); //9 System.out.println("the 345 % 16 is : " + (345 & (16 - 1))); / / 9}Copy the code