Java 中与此等效的是什么?
for (i = (sizeof(num)*8-1); i; i--)
num
是给定的数字,而不是数组。我想反转整数中的位。
最佳答案
Java 没有 sizeof
。数组具有 length
属性,许多集合具有 size()
和类似的东西,但是任何任意对象的语言 sizeof
都是不支持也不需要。
相关问题
- Is there any sizeof-like method in Java?
- In Java, what is the best way to determine the size of an object?
按 LSB-MSB 顺序获取整数位
从 least significant bit 中获取整数的位到其most significant bit ,你可以这样做:
int num = 0xABCD1234;
System.out.println(Integer.toBinaryString(num));
for (int i = 0; i < Integer.SIZE; i++) {
System.out.print((num >> i) & 1);
}
打印:
10101011110011010001001000110100 // MSB-LSB order from toBinaryString
00101100010010001011001111010101 // LSB-MSB order from the loop
因此,在这种特定情况下,sizeof * 8
转换为 Integer.SIZE
,“用于以二进制补码形式表示 int 值的位数”。在 Java 中,该值固定为 32。
JLS 4.2.1 Integral types and values
For
int
, from-2147483648
to2147483647
, inclusive
关于java - 关于sizeof的问题。我想反转数字中的位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2987681/