简体   繁体   English

如何在位掩码中使用有符号整数类型中的每一位?

[英]How do i use every bit in a signed integer type in a bitmask?

I am getting a strange error while attempting to use a java signed long integer to store 64 bits of boolean data.我在尝试使用 java 有符号长整数存储 64 位布尔数据时遇到一个奇怪的错误。 The values are being set and tested correctly, except for every bit where (bits & (1 << n)) >0 is true, a value of n 32 higher is also inexplicably true (nonzero).这些值正在正确设置和测试,除了(bits & (1 << n)) >0为真的每一位,n 32 更高的值也莫名其妙地为真(非零)。

I am thinking this is a result of rollover somehow, but i am not familiar with how java handles sign bits and rollover internally.我认为这是不知何故翻转的结果,但我不熟悉 java 如何在内部处理符号位和翻转。 this same sort of operation has worked fine in other languages with only signed integer types.这种相同的操作在其他只有有符号整数类型的语言中运行良好。

Pseudocode for my bit operations:我的位操作的伪代码:

// this is for manipulating minecraft world chunks directly, if anyone is wondering what the point is. 
// there is really no higher-level alternative that wont use vast amounts of memory and time.
long[][][] bits = new long[16][16][4]; // an array of long bitfields, each representing 64 blocks along Y-axis
                    // 16x16x(4x64) is 65536 blocks, or one chunk
                    // bits[ column-x ][ column-z ][ slices-y ]  - fairly basic format


// set a bit (this all works fine, i have tested the results)
long nb = ( (long)1 << (b.getY()%64) ); // the new bit to be set.
//(a % b + b) % b fixes issues with java modulus giving negative results for negative values of a
bits[(b.getX()%16 + 16)%16][(b.getZ()%16+16)%16][b.getY()/64] |= nb; 
// set the relighting bit for the block


// This is the bug.
//Loops through n=63 to n=0
b = one of the longs from bits[][][]
n=64;
while(--n>-1)
    if( (b & (1<<n)) > 0 )....
//  this evaluates correctly to nonzero for the expected bit, but also for every value of n 32 higher than each expected one.
// IE; if the 4th bit is set, b & (1<<3) is nonzero, but b & (1<<35) also tests nonzero. i have debugged the bitmasks before the operation and confirmed that only the correct bit is set.

// the resulting value of b & (1<<n) is the same for the correct value and for the value 32 bits farther along.

In the if statement towards the end of the code在代码末尾的 if 语句中

(1<<n)

needs to be需要是

(1L<<n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在java中声明32位整数? - How do I declare a 32bit integer in java? 如何在Java中将带符号的16位整数转换为无符号16位整数? - How to convert signed 16 bit integer to unsigned 16 bit integer in Java? 我如何每秒将数字添加到整数 - how do i add a number to an integer every second 如何将带符号的十进制值转换为32位的little-endian二进制字符串? - How do I convert a signed decimal value to a 32 bit little-endian binary string? 如何在签名的applet中使用多个jar? - How do I use multiple jars in a signed applet? 我想为每个已登录的用户显示唯一列表。我该如何做? - I want to show unique list for every user who has signed in. How can I do this? 我的Java程序正在测试整数是否可以被3或5整除,但是仅打印每个整数,如何解决? - My Java program is testing if integer is divisible by 3 or 5, but is just printing every integer, how do I fix it? 如何声明具有适当类型的64位整数的ARRAY_SIZE的arr1 - How can I declare an arr1 of ARRAY_SIZE with the appropriate type 64 bit integer 如何从int转换为泛型Integer? - How do I cast from int to generic type Integer? 如何从十六进制字节中获取带符号的32位整数而不在Java中应用2的补码 - How to get a signed 32 bit integer from hex bytes without being 2's complement applied to it in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM