sign int i = 0x81111111; i = (i >> 31) | (-i >>> 31); System.out.println( i ); reserve highest 1 int i = 0x88000088; i |= (i >> 1); i |= (i >> 2); i |= (i >> 4); i |= (i >> 8); i |= (i >> 16); i -= (i >>> 1); System.out.printf( "0x%X%n", i ); reserve lowest 1 int i = 0x88000088; i &= -i; System.out.printf( "0x%X%n", i ); number of leading 0 int i = 0x88000088; int n = 0; if (i >>> 16 == 0) { n += 16; i <<= 16; } if (i >>> 24 == 0) { n += 8; i <<= 8; } if (i >>> 28 == 0) { n += 4; i <<= 4; } if (i >>> 30 == 0) { n += 2; i <<= 2; } if (i >>> 31 == 0) { n += 1; i <<= 1; } if (i == 0) n++; // n -= i >>> 31; System.out.println( n ); number of 1 int i = 0x81111111; i = ((i&0xAAAAAAAA) >>> 1) + (i&0x55555555); i = ((i&0xCCCCCCCC) >>> 2) + (i&0x33333333); i = ((i&0xF0F0F0F0) >>> 4) + (i&0x0F0F0F0F); i = ((i&0xFF00FF00) >>> 8) + (i&0x00FF00FF); i = ((i&0xFFFF0000) >>> 16) + (i&0x0000FFFF); System.out.println( i ); reverse order int i = 0x11111111; i = ((i&0xAAAAAAAA)>>>1) | ((i&0x55555555)<<1); i = ((i&0xCCCCCCCC)>>>2) | ((i&0x33333333)<<2); i = ((i&0xF0F0F0F0)>>>4) | ((i&0x0F0F0F0F)<<4); i = ((i&0xFF00FF00)>>>8) | ((i&0x00FF00FF)<<8); i = ((i&0xFFFF0000)>>>16) | ((i&0x0000FFFF)<<16); System.out.printf( "0x%X%n", i );翻译代码
时间: 2024-03-28 17:39:21 浏览: 126
这是一些关于位运算的Java代码。下面是每个代码段的翻译:
1.将i初始化为0x81111111,然后对i进行位运算,将i的最高位和最低位进行保留,并输出i的值。
2.将i初始化为0x88000088,然后对i进行位运算,将i的最高位进行保留,并输出i的值。
3.将i初始化为0x88000088,然后对i进行位运算,将i的最低位进行保留,并输出i的值。
4.将i初始化为0x88000088,然后对i进行位运算,计算i的前导零的数量,并输出这个数量。
5.将i初始化为0x81111111,然后对i进行位运算,计算i中1的数量,并输出这个数量。
6.将i初始化为0x11111111,然后对i进行位运算,将i的二进制位按照相反的顺序排列,并输出i的值。
相关问题
如何用java代码实现 openssl dgst -sha256 -sign
使用Java代码实现相当于使用Java的加密库进行签名,可以使用Java Security API中的Signature类。下面是一个示例代码:
```java
import java.io.*;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
public class Signer {
public static void main(String[] args) throws Exception {
String data = "Hello World";
String privateKeyPath = "/path/to/private.key";
byte[] privateKeyBytes = readAllBytes(new FileInputStream(privateKeyPath));
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(data.getBytes());
byte[] signatureBytes = signature.sign();
System.out.println("Signature: " + bytesToHex(signatureBytes));
}
private static byte[] readAllBytes(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int n;
while ((n = input.read(buffer)) != -1) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int i = 0; i < bytes.length; i++) {
int v = bytes[i] & 0xFF;
hexChars[i * 2] = HEX_ARRAY[v >>> 4];
hexChars[i * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
}
```
这个示例代码使用RSA算法进行签名,并使用SHA256算法对数据进行哈希。其中,data是待签名的数据,privateKeyPath是私钥文件的路径。签名结果会以十六进制字符串形式输出。
阅读全文