Java Bit Manipulation

Source: Internet
Author: User

OR (|) and (&) XOR (^) Left Shift (<<) Right Shift (>>) Not (~)
1|0=1 1&0=0 1^0=1 0010<<2=1000 1100>>2=0011 ~1=0
And and &

Num & 1 can get the last digit of the integer num binary expression.

Example:

//Get maximum binary Gap.//For example, 9 's binary form is 1001, and the Gap is 2.//An integer x & 1 would get the last digit of the integer. Public classMaxbinarygap { Public Static intGetgap (intN) {intMax = 0; intCount =-1; intR = 0;  while(N > 0) {            //Get right to most bits & shift Rightr = N & 1; N= N >> 1; if(0 = = R && Count >= 0) {Count++; }            if(1 = =r) {Max= count > Max?Count:max; Count= 0; }        }        returnMax; }}
Xor different or ^

If A and B two values are not the same, then the XOR result is 1. If the A and B two values are the same, the XOR result is 0.

1. a ^ b = b ^ a

2. A ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ C;

Example:

//Given An array of integers, every element appears twice except for one. Find the single one. Public classSinglenumber { Public Static voidMain (string[] args) {int[] A = {1, 2, 3, 3, 2, 1, 5};    System.out.print (Singlenumber (A)); }     Public Static intSinglenumber (int[] A) {intresult = A[0];  for(inti = 1; i < a.length; i++) {System.out.println ("Result:" +result+ "; A[i]: "+ a[i]+"; Result^a[i]: "+ (result^a[i])); Result= result ^A[i]; }        returnresult; }}
<< shift Operators

There are three kinds of shift operators in Java

<<: Left shift operator, Num << n, equals num times 2 n

>>: A signed right-shift operator, Num >>n, which is equal to num divided by 2 n-th square. If it is positive, then the high is 0, is negative, then the high 1 (7 >> 1 result is 3,)

>>>: Unsigned right shift, NUM >>> n, move num to the right n-bit, unsigned right shift, ignore sign bit, empty 0

Example:

2<<2, the binary of 2 is 00000000 00000000 0000000000000010 (1 int = 4 bytes = three bits), which moves 2 bits to the left and becomes 00001000, or 8. If analyzed from another angle, it moves 2 bits to the left, in fact, it is 2 2 times, the result is 8.

SYSTEM.OUT.PRINTLN (integer.tobinarystring (num)); You can view the binary representations of integers.

// Get bit I for a give number n. (I count from 0 and starts by right)  Public Static boolean getbit (intint  i) {    int result = num & (1<<i); c11/>If(result = = 0)        {returnfalse;    } Else {        returntrue;    }}
Binary representation of integers 1) signed and unsigned differences

There are only int types in Java and no unsigned integer and signed.

The number of a signed int in Java is 32 bits, and the range he can represent is between -2^31 ~ 2^31-1. So how do you mean negative numbers? The left side is negative starting with 1.

This causes the shift operators to have "signed" and "unsigned" points. This distinction only exists when moving right. Because it is necessary to indicate whether the left margin after the right shift is 0 or 1, that is, the number after the shift is the negative

2) binary representation of negative numbers

In a computer, negative numbers are expressed in the form of a positive complement.

Original code: An integer, converted to a binary number according to the absolute size, called the original code

For example, 00000000 00000000 00000000 00000101 is the original code of 5.

Inverse code: The binary number is reversed, the resulting new binary number is called the original binary number of the inverse code.

Take the counter-operation means: The original is 1, get 0; 0, 1. (1 change 0; 0 Change 1)

For example: 00000000 00000000 00000000 00000101 Each is reversed, the 11111111 11111111 11111111 11111010.

Complement: The inverse code plus 1 is called the complement.

That is, to get a number of the complement, first get the anti-code, and then add the inverse code 1, the resulting number is called the complement.

For example: 00000000 00000000 00000000 00000101 The inverse code is: 11111111 11111111 11111111 11111010.

Then, the complement is:

11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011

So, 5 is expressed in the computer as: 11111111 11111111 11111111 11111011. Convert to 16 binary: 0xFFFFFFFB.

Example

Integer-1 is represented in the computer.

Assuming this is also an int type, then: 

1, first take 1 of the original code: 00000000 00000000 00000000 00000001

2, the reverse code: 11111111 11111111 11111111 11111110

3. Complement: 11111111 11111111 11111111 11111111

Visible, 1 in the computer with the binary expression is all 1. 16 binary: 0xFFFFFF.

Java Bit Manipulation

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.