Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从无符号整数获取位集位置_C#_Bit Manipulation - Fatal编程技术网

C# 从无符号整数获取位集位置

C# 从无符号整数获取位集位置,c#,bit-manipulation,C#,Bit Manipulation,我有一个来自PDF的标志,它返回一个4096的整数。据我所知,我应该能够测试第13个位置,并使用下面的函数得到一个积极的结果,但它只是给12个位置一个积极的结果?位置是基于零还是什么,所以我需要检查(位置-1) bool GetIsBitSet(int标志,int位置) { return(flags&(1Yes,函数中的position参数是基于零的。这是由于执行测试的方式基本上通过向左移动1pos次数(相当于乘以2pos次数)来设置掩码) 1谢谢-我从PDF工具供应商那里得到了代码,所以我不得

我有一个来自PDF的标志,它返回一个4096的整数。据我所知,我应该能够测试第13个位置,并使用下面的函数得到一个积极的结果,但它只是给12个位置一个积极的结果?位置是基于零还是什么,所以我需要检查(位置-1)

bool GetIsBitSet(int标志,int位置)
{

return(flags&(1Yes,函数中的position参数是基于零的。这是由于执行测试的方式基本上通过向左移动1
pos
次数(相当于乘以2
pos
次数)来设置掩码)


1谢谢-我从PDF工具供应商那里得到了代码,所以我不得不深入了解他的代码不起作用的原因。当我意识到代码到处都是错的时候,我想他没有考虑到零基,所以这只是一个理智的检查。
bool GetIsBitSet(int flags, int pos)
{
    return (flags & (1 << pos)) != 0;
}
 1 << 0   = 1     // tests bit 1   binary: 0000 0000 0000 0001
 1 << 1   = 2     // tests bit 2,  binary: 0000 0000 0000 0010
 1 << 2   = 4     // tests bit 3,  binary: 0000 0000 0000 0100
     ... 
 1 << 12  = 4096  // tests bit 13, binary: 0001 0000 0000 0000