Java bit Operations

Source: Internet
Author: User

/*

Bitwise arithmetic TWOS complement


Basic & (with) | (or) ^ (XOR) ~ (bitwise reversed)


concatenated values (except for Boolean types)

Generally we do the case, the data used are all integers (int)


*/

Class Operatordemo{public static void Main (string[] args) {int a = 3;int B = 4;//bit with &system.out.println (A&B);//0 Bit or System.out.println (a|b),//7//bit XOR System.out.println (a^b),//7//bitwise inversion SYSTEM.OUT.PRINTLN (to);}}

/*

Simulating the underlying operation:

The complement of the binary

int a 4 bytes 32 3

Original Code 00000000 00000000 00000000 00000011



int b 4 bytes 32 4

Original Code 00000000 00000000 00000000 00000100


Bit and operation: 0 0

00000000 00000000 00000000 00000011

& 00000000 00000000 00000000 00000100

------------------------------------------

00000000 00000000 00000000 00000000


Results 0


Bit or operation: there are 1 1

00000000 00000000 00000000 00000011

| 00000000 00000000 00000000 00000100

------------------------------------------

00000000 00000000 00000000 00000111

The result is 7.


Bitwise XOR: Same as 0 different for 1

00000000 00000000 00000000 00000011

^ 00000000 00000000 00000000 00000100

------------------------------------------

00000000 00000000 00000000 00000111


The result is 7.


Bitwise COUNTER ~ 0 Change 1 1 to 0 per bit

00000000 00000000 00000000 00000011

~

-----------------------------------------

11111111 11111111 11111111 11111100 complement


11111111 11111111 11111111 11111011 Anti-code

10000000 00000000 00000000 00000100 Original code

(-4)


*/


/*

Bit XOR or characteristic:

^

A number with another digit XOR two times gets the result is its value itself

*/

Class Operatordemo2{public static void Main (string[] args) {int a = 3;int B = 4; System.out.println (a^b^a);//4 do you get the result B System.out.println (a^b^b);//If I get the result is 3 to illustrate a conclusion: The result is a}}


/*

Bitwise arithmetic TWOS complement

A: Case Demo

<<: Left shifts the binary of the number to the left to fill 0, the removed high bit is discarded.

>>: The right shift is shifted by the binary highest bit is 0, after the right shift, the vacant bit is 0, the highest bit is 1, the high is 1.

>>>: Unsigned Right shift shifted binary highest bit either 0 or 1, the vacant bit is filled with 0.

B: Conclusion:

<<: Multiply << left data by 2 to move power

>>: Divide >> left data by 2 to move power

Interview questions:

Please calculate the result of 2*8 in the most efficient way?

It's the fastest bit operation.

2*8 = 2*2^3

2<<3


64/8

=64/2^3 = 64>>3

*/

Class Operatordemo3{public static void Main (string[] args) {///left data multiplied by 2 of the moving sub-System.out.println (3<<2);//3*2^2 = 12/        /system.out.println (3<<3);//3*2^3 = 24//>> right Shift//left data divided by 2 for moving the second party System.out.println (24>>3);//24/2^3 = 3        System.out.println (24>>>3); System.out.println ("-------------------------");//What if it is a negative number? System.out.println ( -24>>3);//-3 System.out.println ( -24>>>3);//?}}

/*

Simulate the bottom of the computer

3 int 4

00000000 00000000 00000000 00000011 Original code


00000000 00000000 00000000 00000011

<< (00) 000000 00000000 00000000 0000001100


000000 00000000 00000000 0000001100 The complement of the final result

Same as the original code.

12



System.out.println ( -24>>3);//-3

Shift Right: >>

-24 int 4


10000000 00000000 00000000 00011000 Original code

11111111 11111111 11111111 11100111 Anti-code

11111111 11111111 11111111 11101000 complement


11111111 11111111 11111111 11101000

>> 11111111111 11111111 11111111 11101 (000)

Complement 11111111111 11111111 11111111 11101

Anti-code 11111111111 11111111 11111111 11100

Original code 10000000000 00000000 00000000 00011

(-3)


System.out.println ( -24>>>3),//??


10000000 00000000 00000000 00011000 Original code

11111111 11111111 11111111 11100111 Anti-code

11111111 11111111 11111111 11101000 complement


11111111 11111111 11111111 11101000

>>> 00011111111 11111111 11111111 11101 (000) complement


Original code 00011111111 11111111 11111111 11101



*/


/*

To realize the exchange of two integer variables


1: The idea of finding an intermediate amount

Using intermediate variables

*/

Class operatortest{public static void main (String[] args)  {int a =  20;int b = 40;        //   through intermediate variables That's how it's used in development    This is what you have to master today        /*     // Must master the int temp = a;        a=b;         b=temp;        //through XOR           //left  a,b,a   right  a^b  //to remember the conclusion a = a^b;//   a:20^40  b:40b = a^b;//  20^40^40 = 20   b : 20  a:20^40 a = a^b;//  20^40^20 =40    a:40  b:20*/        //understanding of   best Understanding//a = a+b;// a  : 60 b:40         //b = a-b;// 60-40 = 20  b:20//a =  a-b;// 60-20 = 40  a:40        //also has a   A sentence &nbsp, before the interview b =  (A+b)-(a=b);//a:40//   60 -  40 = 20system.out.println ("A:" +a); System.out.println ("B:" +b); System.out.println (0.09 + 0.01);}}


This article from the "Clear Sky" blog, declined reprint!

Java bit Operations

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.