Java 位操作符

Java 位操作符

操作符 构成了任何编程语言的基本组成部分。Java也提供了许多类型的操作符,可以根据需要来执行各种计算和功能,无论是逻辑、算术、关系等。它们是根据它们提供的功能来分类的。下面是几种类型。

  1. 算术运算符
  2. 单数运算符
  3. 赋值运算符
  4. 关系运算符
  5. 逻辑运算符
  6. 三元运算符
  7. 位操作符
  8. 移位操作符

这篇文章解释了所有关于位操作符的知识。

位操作符

位操作符用于对一个数字的各个位进行操作。它们可以用于任何积分类型(char、short、int等)。它们在执行二进制索引树的更新和查询操作时被使用。

现在让我们来看看Java中的每一个位操作符。

1.位向OR (|)

这个运算符是一个二进制运算符,用”|”表示。它返回输入值的逐位OR,也就是说,如果其中一个位是1,它就给出1,否则就显示0。

例子

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise OR Operation of 5 and 7
  0101
| 0111
 ________
  0111  = 7 (In decimal) 

**2.位数与( & **)

这个运算符是一个二进制运算符,用’&’表示。它返回输入值的逐位相加,即如果两个位都是1,它就显示1,否则显示0。

例子

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise AND Operation of 5 and 7
  0101
& 0111
 ________
  0101  = 5 (In decimal) 

3.位数XOR (^)

这个运算符是一个二进制运算符,用’^’表示。它返回输入值的逐位XOR,也就是说,如果相应的位不同,它给出1,否则显示0。

例子

a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)

Bitwise XOR Operation of 5 and 7
  0101
^ 0111
 ________
  0010  = 2 (In decimal) 

4.位数补码(~)

这个运算符是一个单数运算符,用”~”表示。它返回输入值的一补表示,也就是说,所有的位都被颠倒了,这意味着它把每个0变成1,把每个1变成0。

例子

a = 5 = 0101 (In Binary)

Bitwise Complement Operation of 5

~ 0101
 ________
  1010  = 10 (In decimal) 

注意: 编译器将给出该数字的2的补数,即10的2的补数是-6。

// Java program to illustrate
// bitwise operators
 
public class operators {
    public static void main(String[] args)
    {
        // Initial values
        int a = 5;
        int b = 7;
 
        // bitwise and
        // 0101 & 0111=0101 = 5
        System.out.println("a&b = " + (a & b));
 
        // bitwise or
        // 0101 | 0111=0111 = 7
        System.out.println("a|b = " + (a | b));
 
        // bitwise xor
        // 0101 ^ 0111=0010 = 2
        System.out.println("a^b = " + (a ^ b));
 
        // bitwise not
        // ~00000000 00000000 00000000 00000101=11111111 11111111 11111111 11111010
        // will give 2's complement (32 bit) of 5 = -6
        System.out.println("~a = " + ~a);
 
        // can also be combined with
        // assignment operator to provide shorthand
        // assignment
        // a=a&b
        a &= b;
        System.out.println("a= " + a);
    }
}

输出

a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5
// Demonstrating the bitwise logical operators
 
 
class GFG {
    public static void main (String[] args) {
       
        String binary[]={
          "0000","0001","0010","0011","0100","0101",
          "0110","0111","1000","1001","1010",
          "1011","1100","1101","1110","1111"
        };
       
      // initializing the values of a and b
      int a=3; // 0+2+1 or 0011 in binary
      int b=6; // 4+2+0 or 0110 in binary
       
      // bitwise or
      int c= a | b;
       
      // bitwise and
      int d= a & b;
       
      // bitwise xor
      int e= a ^ b;
       
      // bitwise not
      int f= (~a & b)|(a &~b);
      int g= ~a & 0x0f;
       
       
      System.out.println(" a= "+binary[a]);
      System.out.println(" b= "+binary[b]);
      System.out.println(" a|b= "+binary);
      System.out.println(" a&b= "+binary[d]);
      System.out.println(" a^b= "+binary[e]);
      System.out.println("~a & b|a&~b= "+binary[f]);
      System.out.println("~a= "+binary[g]);
    }
}

输出

 a= 0011
 b= 0110
 a|b= 0111
 a&b= 0010
 a^b= 0101
~a & b|a&~b= 0101
~a= 1100

位移运算符(移位运算符)

移位运算符用于将一个数字的位向左或向右移位,从而将该数字分别乘以或除以2。当我们需要将一个数字乘以或除以2时,就可以使用它们。

语法

 number **shift_op** number_of_places_to_shift;

轮班操作工的类型

轮班操作人员进一步分为4种类型。它们是

  1. 有符号的右移操作符(>>)。
  2. 无符号右移操作符(>>>)
  3. 左移位运算符(<<)
  4. 无符号左移运算符(<<)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程