[英]javascript decimal (16 lower bits) to decimal
Sorry but i'm not good at bit conversion. 对不起,但我不擅长转换。 I need to convert decimal -> decimal by lower 16 bit, as this sample.
我需要将十进制 - >十进制转换为低16位,作为此示例。
1:16777237 decimal = 1000015 hex
2:16 lower bits = 0015 hex (each digit in hexadecimal is 4 bits)
3: 0015 hex = 21 decimal (21 is the result i need)
Using 运用
(16777237).toString(16);
i can get the 1000015 hex, my question is how, i get the lower bits as im, not that strong in bits. 我可以得到1000015十六进制,我的问题是如何,我得到低位作为im,而不是那么强的位。 etc. the bedst way to get the result.
获得结果的最基本方式。
You could apply a bitmask with 1111111111111111 2 and get the result with bitwise AND . 您可以应用位掩码1111111111111111 2并按位AND获取结果。
base 2 base 10 base 16
------------------------- -------- -------
1000000000000000000010101 16777237 1000015
& 0000000001111111111111111 65535 ffff
----------------------------- -------- -------
0000000000000000000010101 21 15
console.log(16777237 & ((1 << 16) - 1));
Another solution could be, just to use the remainder operator %
with 2 16 . 另一个解决方案可能是,只使用余数运算符
%
和2 16 。
console.log(16777237 % (1 << 16));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.