Binary

What is binary

Every 2 into a counting rule.

Case:

. 1  public  class Demo01 {
 2  
. 3      public  static  void main (String [] args) {
 . 4          / ** 
. 5           * first saw binary
 . 6           * / 
. 7          int I = 50; // 110010
 . 8          // in println (i ) output time, Java uses
 . 9          // the API (method) to convert binary to decimal string 
10          System.out.println (I); // "50"
 . 11          // the Java can be provided a method toBinaryString
 12          // see the binary data stored in the memory 
13 is          System.out.println (
 14             Integer.toBinaryString(i));
15         for(i=0; i<=50; i++) {
16             System.out.println(
17                 Integer.toBinaryString(i));
18         }
19 
20     }
21 }

Computer and binary relations

Hex

Hex for short (abbreviated) binary because binary writing lengthy \ trouble \ error-prone, because hexadecimal is base the whole power of two, so four can be shortened to a binary hex digital.

Abbreviation rules: binary forward from the rear, each 4 bits of a binary number hexadecimal abbreviated.

Case:

. 1  public  static  void main (String [] args) {
 2      / ** 
. 3       * 2 binary write directly, Java supports after 7
 4       * characteristics, binary data can be written to the beginning 0B
 . 5       * / 
. 6      int n-= 0b110010 ;
 . 7      System.out.println (n-); 
 . 8  
. 9      // binary writing trouble 
10      n-= 0b11010011111101010010111101;
 . 11      // 16 hexadecimal binary short, convenient to use 
12 is  
13 is      int I = 0x77a65fa8 ;
 14      the System.out .println (
 15          Integer.toBinaryString (I));
 16 }

Complement

Computer coding problem solving a negative number (a signed number), which is a core purpose of the fixed number of binary digits, half as a negative fraction.

How will complement binary fixed number of points half as negative?

4-bit binary encoding rules complement an example to explain:

  1. Computing time, more than four times the automatic overflow give up, always keep the digital binary number is four.
  2. 1 is a high negative number, as an integer of 0 to a high
  3. Negative encoded reverse extrapolated by the positive number.
  4. Negative and positive is complementary symmetry: Gu called complement.

Case 1

 1 public static void main(String[] args) {
 2     /**
 3      * 补码
 4      */
 5     int n = -13;
 6     System.out.println(Integer.toBinaryString(n));
 7     for(int i=-50; i<0; i++) {
 8         System.out.println(
 9             Integer.toBinaryString(i)); 
10     }
11 }

Case 2: Special value

 1 public static void main(String[] args) {
 2     int max = Integer.MAX_VALUE; 
 3     int min = Integer.MIN_VALUE;
 4     System.out.println(max);
 5     System.out.println(min);
 6     System.out.println(Integer.toBinaryString(max));
 7     System.out.println(Integer.toBinaryString(min));
 8     //int n = -1;
 9     //int n = 0b11111111111111111111111111111111;
10     int n = 0xffffffff;
11     System.out.println(Integer.toBinaryString(n));
12     System.out.println (n-); // accordance decimal output Levels: -1
 13 is      @ overflow results: It may be positive or may be negative! 
14      int K = 100 ;
 15      System.out.println (K + max); 
 16      System.out.println (max + K + max); 
 . 17      // above described calculation result is an annular coding complement! 
18 }

Complement complementary symmetry:

Calculation principles:

n       00000000 00000000 00000000 00110010   50
~n      11111111 11111111 11111111 11001101  -51
~n+1    11111111 11111111 11111111 11001110  -50

Complementary symmetry test case:

1 int n = 50;
2 int m = ~n + 1;
3 System.out.println(m);//-50
4 System.out.println(Integer.toBinaryString(n));
5 System.out.println(Integer.toBinaryString(~n));
6 System.out.println(Integer.toBinaryString(~n+1));

Classic interview questions:

System.out.println(~5);
如上代码的输出结果:( D ) A.5 B.6 C.-5 D.-6

System.out.println(~-5);
如上代码的输出结果:( A ) A.4 B.5 C.6 D.7

Binary arithmetic

calculating signs:

~ 取反
& 与
| 或
>> 右移位
>>> 逻辑右移位
<< 左移位

And & calculation (logical multiplication)

The basic rule: 0 or 0

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

Computing time, the number of bits required two numbers thereof, and the corresponding location of the digital calculation operations:

For chestnut:

n =    01110111 01010100 10111111 11110111
m =    00000000 00000000 00000000 11111111   mask
k=n&m  00000000 00000000 00000000 11110111 

Significance computed as: above is a mask (Mask) is calculated (resolved calculation), a result which is split n k is the last 8 bits (1 byte)

experiment

int n = 0x7754bff7;
int m = 0xff; //8位掩码(Mask): 1的个数有8个
int k = n&m;
//按照2进制验证结果
System.out.println(Integer.toBinaryString(n));
System.out.println(Integer.toBinaryString(m));
System.out.println(Integer.toBinaryString(k));

>>> Logical right-shift calculation

The binary digits overall shift to the right, the low automatic overflow, high fill 0:

Case:

 1 public static void main(String[] args) {
 2     /**
 3      * 移位计算案例
 4      */
 5     int n = 0x7754bff7;
 6     int m = n>>>1;
 7     int k = n>>>2;
 8     int g = n>>>8;
 9     int b3 = (n>>>8) & 0xff;
10     System.out.println(Integer.toBinaryString(n));
11     System.out.println(Integer.toBinaryString(m));
12     System.out.println(Integer.toBinaryString(k));
13     System.out.println(Integer.toBinaryString(g));
14     System.out.println(Integer.toBinaryString(b3));
15 }

Split byte integer

Case:

. 1  public  static  void main (String [] args) {
 2      / ** 
. 3       * will be split into an integer int four byte
 . 4       * Case: an integer long split eight byte
 . 5       * / 
. 6      int n-= 0x7745abd7 ;
 . 7      int B1 = (n->>> 24) & 0xFF ;
 . 8      int B2 = (n->>> 16) & 0xFF ;
 . 9      int B3 = (n->>>. 8) & 0xFF ;
 10      int B4 & 0xFF = n- ;
 . 11      System.out.println (Integer.toBinaryString (n-));
 12 is      System.out.println (Integer.toBinaryString (B1));
 13 is     System.out.println (Integer.toBinaryString (B2));
 14      System.out.println (Integer.toBinaryString (B3));
 15      System.out.println (Integer.toBinaryString (B4));
 16      / ** 
. 17       * an integer long split eight byte
 18 is       * / 
. 19      long L = 0x76ab3fed723e7828L ;
 20 is      B1 = ( int ) ((L 56 is >>>) & 0xFF );
 21 is      B2 = ( int ) ((48 L >>> ) & 0xFF );
 22 is      B3 = ( int ) ((L >>> 40) & 0xFF );
 23 is      B4 = ( int ) ((L >>> 32) & 0xFF );
 24      int b5 = (int)((l>>>24)& 0xff);
25     int b6 = (int)((l>>>16)& 0xff);
26     int b7 = (int)((l>>>8)& 0xff);
27     int b8 = (int)((l>>>0)& 0xff);
28     //验证...
29     System.out.println(Long.toBinaryString(l)); 
30 }

| OR operation (logical addition)

The basic rule: There is a 1

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

When calculated, the two digit numbers corresponding to bits aligned or calculated.

For chestnut:

n=      00000000 00000000 00000000 10011101
m=      00000000 00000000 11011111 00000000 
k=n|m   00000000 00000000 11011111 10011101

Significance is calculated as: The two numbers n and m are spliced ​​calculated.

verification:

int n = 0x9d;
int m = 0xdf00;
int k = n|m;
//按照2进制输出

<< left shift

The binary number for each bit to the left, the upper overflow, low fill 0

Mathematically calculated shift

Recalling the decimal point moved Computing:

//   89191.   
//  891910.
// 8919100.
// 10进制时候, 数字向左移动一次, 数值扩展10倍

//    110010.   50
//   1100100.  100 = 50<<1
//  11001000.  200 = 50<<2  
// 2进制时候, 数字向左移动一次, 数值扩大2倍

//    110010.   50
//     11001.   25 = 50>>1
//      1100.   12 = 50>>2 向小方向取整数

Case:

/**
 * 移动计算的数学意义
 */
int n = 50;
System.out.println(n);
System.out.println(n<<1);
System.out.println(n<<2);
System.out.println(n<<3);
System.out.println(n>>1);
System.out.println(n>>2);

The difference between >> >>>

>> Known as an arithmetic right shift calculation, the time when the positive (high is 0), the high bit of 0, the time when a negative (upper 1) the high bit of 1, which is a mathematical operation result division, the direction of the small rounding.

>>> Called a logical right-shift calculation, 0s are high regardless of the sign, when a negative result does not conform to the mathematical operation

For chestnut:

n =     11111111 11111111 11111111 11001110   -50
m=n>>1  111111111 11111111 11111111 1100111   -25
g=n>>2  1111111111 11111111 11111111 110011   -13     
k=n>>>1 011111111 11111111 11111111 1100111   比最大值小24

>> Close mathematical operation result is: 2 divided by the direction of the smaller rounded.

>>> Simply the number of bits to the right, the result is not considered a mathematical sense, digitally split merge when using pure right.

Case Interview: Alternative multiplication integer multiple of 2, the use of mathematical shift!

n * 32 可以替换为 ( n<<5 ) 
n / 2 (n>=0) 可以替换为 ( n>>1 )


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/hello4world/p/12094379.html