Javaと「&0xFF」の例
& 0xFF
とは何かを理解する前に、次のことを知っていることを確認してください。
-
ビットごとのAND演算子、link。
-
16進数をバイナリに変換し、10進数をバイナリに変換します。
つまり、& 0xFF
は、常に最後の8ビットを取得するために使用されます。 IPアドレスを10進数に/から変換する例を見てみましょう。
1. IPアドレスを10進数に変換
IpAddressを10進数に変換し、データベースに保存して、計算と比較を改善するのが一般的です。
Testing IP address = 192.168.1.2 Decimal Number = 3232235778
192.168.1.2
を10進数(基数10)に変換するには、式は次のとおりです。
192 x (256)^3 + 168 x (256)^2 + 1 x (256)^1 + 2 (256)^0 = ? 3221225472 + 11010048 + 256 + 2 = 3232235778
P.S A standard IP is “base 256”, source.
2. &0xFFを使用して、10進数をIPアドレスに変換
10進数をIPアドレスに戻すには、ビットシフト演算子を使用し、& 0xff
で「マスク」します。
Javaコード
long ipAddress = 3232235778L; String binary = Long.toBinaryString(ipAddress); System.out.println(binary); System.out.println((ipAddress>>24) & 0xFF); System.out.println((ipAddress>>16) & 0xFF); System.out.println((ipAddress>>8) & 0xFF); System.out.println((ipAddress) & 0xFF);
出力
11000000101010000000000100000010 192 168 1 2
問題は、なぜ(ipAddress>>24) & 0xFF
が192を返すのかということです。 以下のバイナリビットシフト理論に飛び込みましょう。
2.1 (ipAddress>>24) & 0xFF = 192
Decimal = 3232235778 Binary = 11000000 10101000 00000001 00000010 IpAddress = 192 168 1 2 (ipAddress>>24) -------------------------->24 Binary = 00000000 00000000 00000000 11000000 (ipAddress>>24) & 0xFF Binary = 00000000 00000000 00000000 11000000 & 0xFF = 00000000 00000000 00000000 11111111 Result = 00000000 00000000 00000000 11000000 = 192 (2^7 + 2^6)
この場合、0xFFはオプションです。
2.2 (ipAddress>>16) & 0xFF = 168
Decimal = 3232235778 Binary = 11000000 10101000 00000001 00000010 (ipAddress>>16) ----------------->16 Binary = 00000000 00000000 11000000 10101000 (ipAddress>>16) & 0xFF Binary = 00000000 00000000 11000000 10101000 = 49320 (2^14 + 2^15 + 2^7 + 2^5 + 2^3) & 0xFF = 00000000 00000000 00000000 11111111 Result = 00000000 00000000 00000000 10101000 = 168
& 0xFF
の前に49320を取得し、& 0xFF
の後に正しい168を取得します。 これで、& 0xFF
の使用法を理解する必要があります。これにより、常に最後の8ビットを取得できるようになります。
2.3 (ipAddress>>8) & 0xFF = 1
Decimal = 3232235778 Binary = 11000000 10101000 00000001 00000010 (ipAddress>>8) -------->8 Binary = 00000000 11000000 10101000 00000001 (ipAddress>>8) & 0xFF Binary = 00000000 11000000 10101000 00000001 = 12625921 & 0xFF = 00000000 00000000 00000000 11111111 Result = 00000000 00000000 00000000 00000001 = 1
2.4 (ipAddress) & 0xFF = 2
Decimal = 3232235778 Binary = 11000000 10101000 00000001 00000010 (ipAddress) Binary = 11000000 10101000 00000001 00000010 (ipAddress) & 0xFF Binary = 11000000 10101000 00000001 00000010 = 3232235778 & 0xFF = 00000000 00000000 00000000 11111111 Result = 00000000 00000000 00000000 00000010 = 2
3. Javaソースコード
上記のシナリオを示す完全なJavaの例。
BitwiseExample.java
package com.example.core; public class BitwiseExample { public static void main(String[] args) { BitwiseExample obj = new BitwiseExample(); long ipAddressInLong = obj.ipToLong("192.168.1.2"); System.out.println(ipAddressInLong); String binary = Long.toBinaryString(ipAddressInLong); printPrettyBinary(binary); String ipAddressInString = obj.longToIp(ipAddressInLong); System.out.println(ipAddressInString); } public long ipToLong(String ipAddress) { String[] addrArray = ipAddress.split("\\."); long num = 0; for (int i = 0; i < addrArray.length; i++) { int power = 3 - i; // 1. (192 % 256) * 256 pow 3 // 2. (168 % 256) * 256 pow 2 // 3. (2 % 256) * 256 pow 1 // 4. (1 % 256) * 256 pow 0 num += ((Integer.parseInt(addrArray[i]) % 256 * Math.pow(256, power))); } return num; } public String longToIp(long i) { return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + (i & 0xFF); } //print pretty binary code, padding left zero private static void printPrettyBinary(String binary) { String s1 = String.format("%32s", binary).replace(' ', '0'); System.out.format("%8s %8s %8s %8s %n", s1.substring(0, 8), s1.substring(8, 16), s1.substring(16, 24), s1.substring(24, 32)); } }
出力
3232235778 11000000 10101000 00000001 00000010 192.168.1.2