gpt4 book ai didi

java - Java中的按位运算子串

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:42:31 27 4
gpt4 key购买 nike

假设您有一个数字的二进制或十六进制表示。让我们看看这个:

int number  = 0xffffff;
// this would recover the third f, as a stand-alone value, value of third_f would be just f
int third_f = byteSubstring(number,2,1);
// third and fourth f, value of tf would be ff
int tf = byteSubstring(number,2,2);
// all except first, value of except_first would be fffff
int except_first = byteSubstring(number,1,5);

使用单独的按位运算、笔和纸,我知道如何提取所有这些,但将它们组合在一个通用函数中……:)。 JDK 中是否已经提供了可以为数字类型执行此操作的功能?

最佳答案

你有一个 sizeoffset以位指定。传统上,位从 LSB 开始编号。

您处理 offset向右移动

result = x >>> offset

您通过 mask 处理尺寸; (1 << size) - 1是面具

result = result & ((1 << size) - 1)

关于java - Java中的按位运算子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com