写10位数和6位数来缩短?

use*_*902 2 c# bit-manipulation bit-shift

我正在编写一个程序,其中一个数字(一个10位数字)存储在一个16位数字的第一部分,然后一个6位数字存储在最后.

如何利用位移来实现我的目标?

Mar*_*ell 8

注意:我将"16位数字的第一部分"解释为"10个最低有效位" - 因为位数学通常从右侧向后计数.

short x = (short)(value & 1023); // the first 10 bits
short y = (short)((value >> 10) & 63); // the last 6 bits
Run Code Online (Sandbox Code Playgroud)

并重新组合:

value = (short)(x | (y << 10));
Run Code Online (Sandbox Code Playgroud)