2013年12月21日 星期六

Binary Literals
許裕永 譯 (原文內容取自 Oracle 官網)
Binary Literals - In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number.
 二進位字面值 - 在 Java SE 7 , 整數型別 (byte, short, int, and long) 可以使用二進位數字系統表示. 要撰寫二進位的字面值, 以 0b 或 0B 為數字的字首.

The following examples show binary literals:
下列範例示範二進位字面值的表示方式:

// An 8-bit 'byte' value: byte aByte = (byte)0b00100001;
// A 16-bit 'short' value: short aShort = (short)0b1010000101000101;
// Some 32-bit 'int' values: int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
 int anInt3 = 0B101;
 // The B can be upper or lower case.
 // A 64-bit 'long' value. Note the "L" suffix:
 long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;


Binary literals can make relationships among data more apparent than they would be in hexadecimal or octal. For example, each successive number in the following array is rotated by one bit:
二進位字面值可以讓資料之間的關係比使用16進位或8進位更加顯而易見. 範例中, 各個連續的數字於陣列中以一個 bit 往前進位:

public static final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001, 0b00010011, 0b00100110, 0b01001100, 0b10011000 }
In hexadecimal, the relationship among the numbers is not readily apparent:
在16進位中, 數字之間的關係便不是很容易顯示出來:

public static final int[] phases = { 0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98 } You can use binary integral constants in code that you can verify against a specifications document, such as a simulator for a hypothetical 8-bit microprocessor:
你可以把二進位數字包含在程式碼中, 讓你可以與規格文件進行比對驗證, 如同8位元微處理器一般.

public State decodeInstruction(int instruction, State state) {
  if ((instruction & 0b11100000) == 0b00000000) {
  final int register = instruction & 0b00001111;
  switch (instruction & 0b11110000) {
   case 0b00000000:
    return state.nop();
   case 0b00010000:
    return state.copyAccumTo(register);
   case 0b00100000:
    return state.addToAccum(register);
   case 0b00110000:
    return state.subFromAccum(register);
   case 0b01000000:
    return state.multiplyAccumBy(register);
   case 0b01010000:
    return state.divideAccumBy(register);
   case 0b01100000:
    return state.setAccumFrom(register);
   case 0b01110000:
    return state.returnFromCall();
   default:
    throw new IllegalArgumentException();
  }
 } else {
  final int address = instruction & 0b00011111;
  switch (instruction & 0b11100000) {
   case 0b00100000:
    return state.jumpTo(address);
   case 0b01000000:
    return state.jumpIfAccumZeroTo(address);
   case 0b01000000:
    return state.jumpIfAccumNonzeroTo(address);
   case 0b01100000:
    return state.setAccumFromMemory(address);
   case 0b10100000:
    return state.writeAccumToMemory(address);
   case 0b11000000:
    return state.callTo(address);
   default: throw new IllegalArgumentException();
  }
 }
}


 You can use binary literals to make a bitmap more readable:
你可以使用二進位字面值讓一個圖檔更加容易讀取.

public static final short[] HAPPY_FACE = {
 (short)0b0000011111100000;
 (short)0b0000100000010000;
 (short)0b0001000000001000;
 (short)0b0010000000000100;
 (short)0b0100000000000010;
 (short)0b1000011001100001;
 (short)0b1000011001100001;
 (short)0b1000000000000001;
 (short)0b1000000000000001;
 (short)0b1001000000001001;
 (short)0b1000100000010001;
 (short)0b0100011111100010;
 (short)0b0010000000000100;
 (short)0b0001000000001000;
 (short)0b0000100000010000;
 (short)0b0000011111100000;

 

沒有留言:

張貼留言