声明用于按位操作的掩码 [英] Declaring masks for bitwise operations

查看:80
本文介绍了声明用于按位操作的掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这种低级操作的新手,我希望有人可以指出我在这里必须犯的明显错误.

I'm new to low level operations like this, I'm hoping someone can point out the obvious mistake I must be making here.

//Input value - 00111100
//I want to get the value of the bits at indexes 1-3 i.e 0111.

byte mask = (byte)0x00001111; // This gives 17 not the 15 I'd expect 

byte shifted = (byte)(headerByte >> 3);
//shifted is 7 as expected

byte frameSizeValue = (byte)(shifted & mask); //Gives 1 not 7

问题似乎出在掩码的定义方式上,但我看不到如何解决.

It looks like the problem lies with the way the mask is defined, but I can't see how to fix it.

推荐答案

您的掩码必须为 binary 00001111,它等于十六进制0x0F.

Your mask needs to be binary 00001111, which is equal to hex 0x0F.

byte mask = (byte)0x0F;

这篇关于声明用于按位操作的掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆