[JavaSE] bit operators (& amp; | ^) and javase Operators

Source: Internet
Author: User

[JavaSE] bit operators (& | ^) and javase Operators

Bitwise operations directly calculate binary data

Shift left <

Shift right>

 

First, replace the integer with four 8bit 0000-0000 0000-0000 0000-0000 0000-0000

This binary is moved left and right, and the removed end is filled with 0.

 

Rule:

6 <3 means 6 multiplied by the power of 2

6> 3 means dividing 6 by the power of 2

 

& Operation

| Or operation

^ Exclusive or operation

First, convert the decimal integer to binary.

6 & 3 means that the corresponding positions are consistent. If the values are 1, 1 is required, and the other values are 0.

6 | 3 means that the corresponding positions are the same, as long as 1 is 1, all 0 will get 0

6 ^ 3 means that the corresponding positions are the same, the upper and lower values are the same, and the lower and lower values are 0, and the difference is 1.

 

110

011 & Computation

010 = 2

 

110

011 | or operation

111 = 7

 

110

011 ^ exclusive or

101 = 5

 

These bitwise operations can be used for encryption.

Public class VariableDemo {/*** @ param args */public static void main (String [] args) {System. out. println (6 <3); // output 48 System. out. println (6> 3); // outputs 0 System. out. println (6 & 3); // output 2 System. out. println (6 | 3); // output 7 System. out. println (6 ^ 3); // output 5 }}

 

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.