本文共 1412 字,大约阅读时间需要 4 分钟。
计算器的二进制功能java
Binary literals are new features in Java 7. As you all know that we can write integral types (byte, short, int, and long) in Binary and Hexadecimal formats. However, from Java 7 onwards we can write these numbers in binary format also. The number should be prefixed with 0b or 0B to be treated as binary literal.
二进制文字是Java 7中的新功能。众所周知,我们可以用二进制和十六进制格式编写整数类型(字节,短,整数和长整数)。 但是,从Java 7开始,我们也可以用二进制格式编写这些数字。 该数字应以0b或0B为前缀,以将其视为二进制文字。
This feature is very helpful to bit-oriented systems like processors, network protocols and bitmapped hardware device. Earlier the programmers used to transform from binary to decimal/hexadecimal and vice versa. Using this feature will remove this transformation and chances of error will be less in this conversion.
此功能对于面向位的系统(例如处理器,网络协议和位图硬件设备)非常有帮助。 以前,程序员曾经从二进制转换为十进制/十六进制,反之亦然。 使用此功能将删除此转换,并且此转换中出错的机会会减少。
Also, the code using bitwise operations will be more readable with this feature.
而且,使用按位运算的代码将通过此功能更易读。
Let’s see binary literals in action with a simple java program:
让我们看一下使用简单的Java程序执行的二进制文字:
package com.journaldev.util;public class Java7Literals { public static void main(String[] args) { int i=0b0111; byte b=(byte) 0b0111; long l=(long) 0B0111L; System.out.println("i="+i); System.out.println("b="+b); System.out.println("l="+l); }}
Output of the above program is:
上面程序的输出是:
i=7b=7l=7x=7
翻译自:
计算器的二进制功能java
转载地址:http://mklzd.baihongyu.com/