简体   繁体   English

使用 Java 中的位移位获取存储的数字

[英]getting stored numbers using Bit Shifting in Java

I'm having trouble trying to reverse engineer this section of code;我在尝试对这部分代码进行逆向工程时遇到了麻烦; I need to be able to get x, y, z from l would anyone be able to point me in the right direction.我需要能够从 l 获得 x、y、z 任何人都能够指出我正确的方向。 Thanks谢谢

 int l = ((X << 20) + (Y << 19) + Z);

The ranges are as follows范围如下

X = 0 - 4095
Y = 0 - 1
Z = 0 - 384,794

X being 0-4095 = 12 bits X 为 0-4095 = 12 位

Y being 0-1 = 1 bit Y 为 0-1 = 1 位

Z being 0-384794 = 19 bits Z 为 0-384794 = 19 位

Java integers are 32 bit, so starting from 0: Java 整数是 32 位的,所以从 0 开始:

int l = 0000 0000 0000 0000 0000 0000 0000 0000

+ X << 20 = the value of X, moved 20 places to the right. + X << 20 = X 的值,向右移动 20 位。 So:所以:

int l = XXXX XXXX XXXX 0000 0000 0000 0000 0000

+ Y << 19 = the value of Y, moved 19 places to the right. + Y << 19 = Y 的值,向右移动了 19 位。 So:所以:

int l = XXXX XXXX XXXX Y000 0000 0000 0000 0000

+ Z = the value of Z, moved 0 places. + Z = Z 的值,移动了 0 个位置。 So:所以:

int l = XXXX XXXX XXXX YZZZ ZZZZ ZZZZ ZZZZ ZZZZ

Since no bits are ever overwritten, we can recover them:由于没有位被覆盖,我们可以恢复它们:

int x = l >> 20 & 0xFFF;   //reverse the shifting (by 20), then isolate the X bits (0xFFF = 12 bits set to 1, equal to 4095)
int y = l >> 19 & 0x1;     //reverse the shifting (by 19), then isolate the Y bit (0x1 = 1 bit set to 1, equal to 1)
int z = l & 0x7FFFF;       //no shifting to reverse, but we still isolate the Z bits (0x7FFFF = 19 bits set to 1, equal to 524287)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM