Java Reference
In-Depth Information
xxxxxxxx
user input
bitwise AND
10000000
mask
?0000000
result
bit tested
Figure8-3ANDTestingaSingleBit
OntheWeb
ThesourcefilefortheprogramBit7And.javaisfoundintheChapter8
folderatwww.crcpress.com.
// File name: Bit7And.java
// Reference: Chapter 8
//
// Java program to demonstrate the action of the bitwise
// AND operator
// Topics:
// 1. Using the bitwise AND to determine the state
// of an operand bit
// Requires:
//
1. Keyin class in the current directory
public class Bit7And
{
public static void main(String[] args)
{
// Local variables
int mask = 128;
int userInput;
int result = 0;
// Processing
userInput = Keyin.inInt(“Enter value: ”);
result = userInput & mask;
// If bit 7 was set in the user input, then
// result = 128. Otherwise, result = 0
System.out.println(“result=”+result);
}
}
The | operator
The|operatorperformstheBooleaninclusiveORoftwooperands.Theout-
comeisthatabitintheresultissetifatleastoneofthecorrespondingbits
intheoperandsisalsoset,asshownbythetruthtablein Table8-3 . Afre -
 
Search WWH ::




Custom Search