Java 演示使用二进制字面量
二进制字面量是使用二进制位0和1来表示的数字。在数据类型byte、int、long和short中,可以很容易地用二进制数系统表示数值。
在整数前面加上0b或0B来声明一个二进制字面量。
让我们看一些示例以更好地理解这个主题。
示例
以下程序显示了分配二进制字面量值的byte数据类型的值。在BinaryLiteral1类中声明了2字节数据类型变量,并分配了二进制字面量值,然后显示出来。
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的类中声明了两个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的类内部,声明了两个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
结论
本文讨论了二进制文本的使用。文章中讨论了在字节、短整型、整型和长整型数据类型中显示二进制文本的程序。此外,还介绍了如何在二进制文本上执行不同的数学运算的实现。