Java 程序:执行位运算

原文:https://www.studytonight.com/java-programs/java-program-to-perform-bitwise-operations

在本教程中,我们将学习如何通过从用户处获取输入来执行按位运算。按位运算符是用于操作数字的各个位的运算符。这些在对二进制索引树执行更新和查询操作时非常有用。它适用于Integer类型,长整型、整型、短整型、字符型和字节型。但是在继续之前,如果您不熟悉 java 中按位运算符的概念,那么一定要查看关于 Java 中运算符的文章。

输入: ( 5 & 4)

( 5 | 4 )

( 5 ^ 4 )

( 5 << 4 )

( 5 >> 4 )

输出:

four

five

one

Eighty

Zero

上述问题出现了两种情况:

情况 1:当值由用户定义时

情况 2:当值被预定义时

让我们分别看看这些案例。

程序 1:执行位操作

在这个程序中,我们将看到当值是用户定义的时,如何在 java 中执行位操作。这里,我们首先要求用户输入值,然后执行按位运算。

算法:

  1. 开始
  2. 这里,我们将使用 switch case 从不同的按位运算符中进行选择,如&、|、^、~、<>。
  3. 为相同的声明一个变量。
  4. 请用户初始化它。
  5. 根据选择的操作,声明两个变量。
  6. 要求用户初始化变量。
  7. 执行按位运算后显示结果。
  8. 停下来。

让我们看看下面的例子,以便更好地理解。

//Java Program to perform the bitwise operation
import java.util.Scanner;
public class Main
{
   public static void main(String args[])
   {   
       //Take input from the user
       //Create instance of the Scanner class
        Scanner s = new Scanner(System.in);
        while(true)
        {
            System.out.println("");

            System.out.println("Choose the operation you want to perform ");
            System.out.println("Choose 1 for & ");
            System.out.println("Choose 2 for | ");
            System.out.println("Choose 3 for ^ ");
            System.out.println("Choose 4 for ~");
            System.out.println("Choose 5 for <<");
            System.out.println("Choose 6 for >>");
            System.out.println("Choose 7 for EXIT");
            int n = s.nextInt();
            switch(n)
            {
                case 1:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int x = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int y = s.nextInt();
                    System.out.println("Result of "+x+"&"+y+" = " + (x&y));
                    break;

                case 2:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int p = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int q = s.nextInt();
                    System.out.println("Result of "+p+"|"+q+" = " + (p |q ));
                    break;

                case 3:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int a = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int b = s.nextInt();
                    System.out.println("Result of "+a+"^"+b+" = " + (a ^ b));
                    break;

                case 4:
                    System.out.print("Enter the number : ");
                    int c = s.nextInt();
                    System.out.print("The result of ~"+c+ " is "+(~c));
                    break;

                case 5:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int e = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int f = s.nextInt();
                    System.out.println("Result of "+e+"<<"+f+" = " + (e<<f));
                    break;

                case 6:
                    System.out.println("Enter the two numbers to perform operations ");
                    System.out.print("Enter the first number : ");
                    int g = s.nextInt();
                    System.out.print("Enter the second number : ");
                    int h = s.nextInt();
                    System.out.println("Result of "+g+">>"+h+" = " + (g>>h));
                    break;
                case 7:
                    System.exit(0);
            }
        }
    }
}

选择要执行的操作 选择 1 为& 选择 2 为| 选择 3 为^ 选择 4 为~ 选择 5 为< < 选择 6 为> > 选择 7 为退出 1 输入两个数字执行操作 输入第一个数字:2 输入第二个数字:3 结果为 2 & 3 = 2

选择要执行的操作 选择 1 为& 选择 3 为^ 选择 4 为~ 选择 5 为< < 选择 6 为> > 选择 7 为退出 2 输入两个数字执行操作 输入第一个数字:4 输入第二个数字:5 结果为 4|5 = 5

选择要执行的操作 选择 1 为& 选择 3 为^ 选择 4 为~ 选择 5 为< < 选择 6 为> > 选择 7 为退出 3 输入两个数执行操作 输入第一个数:5 输入第二个数:6 结果 5^6 = 3

选择要执行的操作 选择 1 为& 选择 2 为 选择 3 为^ 选择 4 为~ 选择 5 为< < 选择 6 为> > 选择 7 为退出 4 输入数字:结果~7 为-8 选择要执行的操作 选择 1 为& 选择 2 为| 选择 3 为^ 选择 4 为~ 选择 5 为< < 选择 6 为> > 选择 7 为退出 5 输入两个数进行运算 输入第一个数:8 输入第二个数:9 结果为 8 < < 9 = 4096

选择 3 为^ 选择 4 为~ 选择 5 为< < 选择 6 为> > 选择 7 为退出 6 输入两个数进行运算 输入第一个数:1 输入第二个数:2 结果为 1>>2 = 0 t80 选择 3 为^ 选择 4 为~ 选择 5 为< <选择 6 为> >选择 7 为退出 7

程序 2:执行位操作

在这个程序中,我们将在程序中预定义值时执行按位运算。

算法:

  1. 开始
  2. 这里,我们将使用 switch case 从不同的按位运算符中进行选择,如&、|、^、~、<>。
  3. 声明两个变量。
  4. 初始化它。
  5. 执行所有按位运算符,如&、|、^、~、<>。
  6. 显示每个按位运算的结果。
  7. 停下来。

让我们看看下面的例子,以便更好地理解。

//Java Program to perform bitwise operation
public class Main 
{
    public static void main(String[] args)
    {
        // Declare and initialize the variables
        int a = 3;
        int b = 2;
        // bitwise and
        System.out.println("Result of "+a+"&"+b+" = " + (a & b));
        // bitwise or
        System.out.println("Result of "+a+"|"+b+" = " + (a | b));
        // bitwise xor
        System.out.println("Result of "+a+"^"+b+" = " + (a ^ b));
        System.out.println("Result of ~"+a+" = " + ~a);
        System.out.println("Result of "+a+" << "+b+" = " + (a << b));
        System.out.println("Result of "+a+" >> "+b+" = " + (a >> b));
        // When combined with assignment operator 
        a &= b;
        System.out.println("Result after a&=b is a= " + a);
    }
}

3&2 = 2 的结果 3|2 = 3 的结果 3^2 = 1 的结果~3 = -4 的结果 3 <的结果 T9】 2 = 12 的结果 3 > > 2 = 0 的结果 a &之后=b 为 a= 2