How Does The Bitwise & (AND) Work In Java?

前提是你 提交于 2020-05-19 08:30:13

问题


I was reading through some code examples and came across a & on Oracle's website on their Bitwise and Bit Shift Operators page. In my opinion it didn't do too well of a job explaining the bitwise &. I understand that it does a operation directly to the bit, but I am just not sure what kind of operation, and I am wondering what that operation is. Here is a sample program I got off of Oracle's website: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/examples/BitDemo.java


回答1:


An integer is represented as a sequence of bits in memory. For interaction with humans, the computer has to display it as decimal digits, but all the calculations are carried out as binary. 123 in decimal is stored as 1111011 in memory.

The & operator is a bitwise "And". The result is the bits that are turned on in both numbers. 1001 & 1100 = 1000, since only the first bit is turned on in both.

The | operator is a bitwise "Or". The result is the bits that are turned on in either of the numbers. 1001 | 1100 = 1101, since only the second bit from the right is zero in both.

There are also the ^ and ~ operators, that are bitwise "Xor" and bitwise "Not", respectively. Finally there are the <<, >> and >>> shift operators.


Under the hood, 123 is stored as either 01111011 00000000 00000000 00000000 or 00000000 00000000 00000000 01111011 depending on the system. Using the bitwise operators, which representation is used does not matter, since both representations are treated as the logical number 00000000000000000000000001111011. Stripping away leading zeros leaves 1111011.




回答2:


It's a binary AND operator. It performs an AND operation that is a part of Boolean Logic which is commonly used on binary numbers in computing.

For example:

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

You can also perform this on multiple-bit numbers:

01 & 00 = 00
11 & 00 = 00
11 & 01 = 01
1111 & 0101 = 0101
11111111 & 01101101 = 01101101
...



回答3:


If you look at two numbers represented in binary, a bitwise & creates a third number that has a 1 in each place that both numbers have a 1. (Everywhere else there are zeros).


Example:
0b10011011 &
0b10100010 =
0b10000010


Note that ones only appear in a place when both arguments have a one in that place.
Bitwise ands are useful when each bit of a number stores a specific piece of information.
You can also use them to delete/extract certain sections of numbers by using masks.




回答4:


If you expand the two variables according to their hex code, these are:

bitmask : 0000 0000 0000 1111
val:      0010 0010 0010 0010

Now, a simple bitwise AND operation results in the number 0000 0000 0000 0010, which in decimal units is 2. I'm assuming you know about the fundamental Boolean operations and number systems, though.




回答5:


Its a logical operation on the input values. To understand convert the values into the binary form and where bot bits in position n have a 1 the result has a 1. At the end convert back.

For example with those example values:

0x2222 =  10001000100010
0x000F =  00000000001111
result =  00000000000010   => 0x0002 or just 2



回答6:


Knowing how Bitwise AND works is not enough. Important part of learning is how we can apply what we have learned. Here is a use case for applying Bitwise AND. Example:

Adding any even number in binary with 1's binary will result in zeros. Because all the even number has it's last bit(reading left to right) 0 and the only bit 1 has is 1 at the end.

If you were to ask write a function which takes an argument as a number and returns true for even number without using addition, multiplication, division, subtraction, modulo and you cannot convert number to string.

This function is a perfect use case for using Bitwise AND. As I have explained earlier. You ask show me the code? Here is the java code.

/**
 * <p> Helper function </p>
 * @param number
 * @return 0 for even otherwise 1
 */

private int isEven(int number){
    return (number & 1);
}



回答7:


import.java.io.*;
import.java.util.*;

public class Test {
    public static void main(String[] args) {
        int rmv,rmv1;

        //this R.M.VIVEK complete bitwise program for java
        Scanner vivek=new Scanner();
        System.out.println("ENTER THE X value");
        rmv = vivek.nextInt();
        System.out.println("ENTER THE y value");
        rmv1 = vivek.nextInt();

        System.out.println("AND table based\t(&)rmv=%d,vivek=%d=%d\n",rmv,rmv1,rmv&rmv1);//11=1,10=0
        System.out.println("OR table based\t(&)rmv=%d,vivek=%d=%d\n",rmv,rmv1,rmv|rmv1);//10=1,00=0
        System.out.println("xOR table based\t(&)rmv=%d,vivek=%d=%d\n",rmv,rmv1,rmv^rmv1);
        System.out.println("LEFT SWITH based to %d>>4=%d\n",rmv<<4);
        System.out.println("RIGTH SWITH based to %d>>2=%d\n",rmv>>2);

        for(int v=1;v<=10;v++)
            System.out.println("LIFT SWITH based to (-NAGATIVE VALUE) -1<<%d=%p\n",i,-1<<1+i);
    }
}


来源:https://stackoverflow.com/questions/17256644/how-does-the-bitwise-and-work-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!