[英]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.