This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes & | with integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "& | with integers" Watch "& | with integers" New topic
Author

& | with integers

deva raj
Greenhorn

Joined: May 28, 2004
Posts: 6
public class test {
public static void main(String args[]) {
int x,y;
x = 3 & 5;
y = 3 | 5;
System.out.println(x + " " + y);
}
}

Can anyone explain how this works?


Note: - New to java.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm moving this to Java in General (Beginner).

Check out the JLS, §15.22.1 Integer Bitwise Operators &, ^, and |.


SCJP Tipline, etc.
Jeffrey Bennett
Greenhorn

Joined: May 17, 2004
Posts: 12
x = 3 & 5;

In binary notation, this translates as:
x = (011 & 101);

A bitwise AND operation yields:
x = (001)
x = 1;

----------------
y = 3 | 5;
y = (011) | (101)
y = (111); // <-- Bitwise OR operation
y = 7;
 
GeeCON Prague 2014
 
subject: & | with integers