TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Java Bitwise AND operator

Overview

In this tutorial, we will learn about Java Bitwise AND operator with a simple java program. Bitwise AND operator produce 1 if the corresponding bits in both the operands are 1; otherwise, it produces zero. The (&) is the bitwise AND operation symbol.

Table

Outcomes table with bitwise AND operator are shown as follows:

Operand: A Operand: B Bitwise AND : A&B
0 0 0
0 1 0
1 0 0
1 1 1

Code

Sample Java program to demo the operator usage.

/* 
 * Java Program to demonstrate 
 * Bitwise AND Operator.
 * Java Tutorials -- www.TestingDocs.com 
 */
public class BitwiseANDDemo {
 public static void main(String args[])
 {
 //Declare two variables
 int A = 1;
 int B = 0;
 System.out.println("A & B =: " + (A&B));
 A = 1;
 B = 1;
 System.out.println("A & B =: " + (A&B));
 }
}

Sample output

A & B =: 0
A & B =: 1

 

Java Bitwise AND Operator

Notice that the result is 1 if the individual bits had a 1 for both the operands A and B.

Exercise

1. The above program displays the two rows of the outcome table. Modify the A and B values to check the other rows of the outcome table.

2. Run the following program and check the output. The program masks the bits of the variable A except the lower 4 bits. 0x0F is a hexadecimal number which is equivalent to 15 in the decimal number format.
It represents the lower four bits and translates to the bit pattern 0000 1111.

 

public class BitwiseExerciseDemo {
 public static void main(String args[])
 {
 //Declare two variables
 int A = 173;
 int bitmask = 0x000F; //mask other bits except the lower 4 bits
 System.out.println("Result =: " + (A & bitmask));
 }
}

 

Bitwise_Java_AND_Exercise

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java Bitwise Shift Operators› Java Bitwise OR operator

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version