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 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.