Java程序:二进制字面量的使用示例


二进制字面量是用二进制数字(0和1)表示的数字。 byte、int、long和short等数据类型的值可以很容易地用二进制数表示。

在整数前面添加前缀0b或0B来声明二进制字面量。

让我们看一些例子来更好地理解这个主题。

示例

下面的程序显示一个byte类型变量的值,该变量被赋予了二进制字面量的值。 创建一个名为BinaryLiteral1的类,其中声明并赋值了2个byte类型变量的二进制字面量值,并显示这些值。

public class BinaryLiteral1 {
   public static void main(String[] args) {
      // Binary literal in a byte type
      byte bt1 = 0b1001; // Using upper case 0b
      byte bt2 = 0B1001; // Using lower case 0B
      System.out.println("Illustrating the usage of Binary Literal in Byte data type");
      System.out.println("Value of variable bt1 = "+bt1);
      System.out.println("Value of variable bt2 = "+bt2);
   }
}

输出

Illustrating the usage of Binary Literal in Byte data type
Value of variable bt1 = 9
Value of variable bt2 = 9

示例

下面的程序显示一个short类型变量的值,该变量被赋予了二进制字面量的值。 创建一个名为BinaryLiteral2的类,其中声明并赋值了2个short类型变量的二进制字面量值,并显示这些值。

public class BinaryLiteral2 {
   public static void main(String[] args) {
      // Binary literal in short type
      short n1 = 0b1001; // Using upper case b0
      short n2 = 0B1001; // Using lower case B0
      System.out.println("Illustrating the usage of Binary Literal in short data type");
      System.out.println("The value of variable n1 = "+n1);  
      System.out.println("The value of variable n2 = "+n2);   
   }
}

输出

Illustrating the usage of Binary Literal in short data type
The value of variable n1 = 9
The value of variable n2 = 9

示例

下面的程序显示一个int类型变量的值,该变量被赋予了二进制字面量的值。 创建一个名为BinaryLiteral3的类,其中声明并赋值了2个int类型变量的二进制字面量值,并显示这些值。

public class BinaryLiteral3 {
   public static void main(String[] args) {
      // Binary literal in int type
      int n1 = 0b1001; 
      // Usage of upper case b
      int n2 = 0B1001; 
      // Usage of lower-case B
      System.out.println("Illustrating the usage of Binary Literal in int data type");
      System.out.println("The value of variable st1 = "+n1);  
      System.out.println("The value of variable st2 = "+n2);  
       
    }
}

输出

Illustrating the usage of Binary Literal in int data type
The value of variable st1 = 9
The value of variable st2 = 9

示例

下面的程序显示一个long类型变量的值,该变量被赋予了二进制字面量的值。 创建一个名为BinaryLiteral4的类,其中声明并赋值了2个long类型变量的二进制字面量值,并显示这些值。

public class BinaryLiteral4 {
   public static void main(String[] args) {
      // Binary literal in long type
      long n1 = 0b1001;    // Using upper case b0
      long n2 = 0B1001;    // Using lower case B0
      System.out.println("Illustrating the usage of Binary Literal in long data type");
      System.out.println("The value of variable n1 = "+n1);  
      System.out.println("The value of variable n2 = "+n2);  
       
   }
}

输出

Illustrating the usage of Binary Literal in long data type
The value of variable n1 = 9
The value of variable n2 = 9

示例

下面的Java程序演示了如何在二进制字面量上执行数学运算。 创建一个名为BinaryLiteral5的类,其中对正负二进制字面量执行不同的数学运算。

public class BinaryLiterals5 {  
   public static void main(String[] args) {
      // Declaring a decimal value 
      byte n1 = 26;  
      // Declaring a positive binary literal
      byte n2 = 0b1001;      
      // Declaring a negative binary literal
      byte n3 = -0b1001;  
      // Declaring a negative binary literal Using an underscore 
      byte n4 = -0b1001_0;  
      // Declaring a positive binary literal using an underscore 
      byte n5 = 0b1001_00;  
      // Declaring a positive binary literal using an underscore 
      byte n6 = 0b101_00;  
      //displaying the values of above declared variables
      System.out.println(" The value of variable n1 = "+n1);  
      System.out.println("The value of variable n2 = "+n2);  
      System.out.println("The value of variable n3 = "+n3);  
      System.out.println("The value of variable n4 = "+n4);  
      System.out.println("The value of variable n5 = "+n5);
      System.out.println("The value of variable n6 = "+n6);

      // Check if the binary values present in n2 and n3 are equal   
      System.out.println(" Are the values of n2 and n3 same: "+(n2 == n3));  
      // Performing mathematical operations on binary values  
      System.out.println("n2 + 1 = "+(n2 + 1));  
      System.out.println("n2 - 1 = "+(n2 - 1));  
      System.out.println("n3 * 2 = "+(n3 * 2));  
      System.out.println("n3 / 2 = "+(n5 / n4)); 
      System.out.println("n5 - n6 = "+(n5 - n6));  
   }
}  

输出

The value of variable n1 = 26
The value of variable n2 = 9
The value of variable n3 = -9
The value of variable n4 = -18
The value of variable n5 = 36
The value of variable n6 = 20
 Are the values of n2 and n3 same: false
n2 + 1 = 10
n2 - 1 = 8
n3 * 2 = -18
n3 / 2 = -2
n5 - n6 = 16

结论

本文阐述了二进制字面量的用法。 本文讨论了分别用二进制字面量显示byte、short、int和long数据类型值的程序。 此外,本文还讨论了如何对二进制字面量执行不同数学运算的实现。

更新于:2023年4月12日

423 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告