获得从字节的特定位 [英] Get a specific bit from byte

查看:196
本文介绍了获得从字节的特定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节,从另一设备发送其排在通过UDP字节数组专门一个字节。这个字节存储在装置8的继电器的开/关状态。

我如何得到字节表示一个特定位的值?理想的情况是一个扩展方法看起来最优雅并返回一个布尔将使最有意义的我。

 公共静态布尔GETBIT(此字节B,诠释位编号)
{
    //黑色魔高一尺这​​里
}


解决方案

易。使用按位与您的带有值比较2 ^位编号,可以通过位移来计算便宜

  //你的黑魔法
VAR位=(B&安培;(1<<位编号-1))!= 0;

编辑::要添加更多的细节,因为有很多相似的答案,没有解释:

按位与比较每个号码逐位,使用和加入,产生一个数字,其中两个在那个地方第一位和第二位分别设置​​位的组合。这里的逻辑矩阵和逻辑,显示了按位操作的蚕食和:

  0101
&安培; 0011
  ----
  0001 //只有最后位被设置,因为只有这两个被加数的最后位被设置

在你的情况,我们比较你一个数字,只有你想寻找一套该位通过数字。比方说,你要寻找的第四位:

  11010010
&安培; 00001000
  --------
  00000000 // == 0,所以位未设置  11011010
&安培; 00001000
  --------
  00001000 //!= 0,因此该位被置

位移,以产生我们要比较的次数,正是这听起来像:取psented作为一组的比特数,再$ P $,以及这些比特向左或向右移动以一定数目的地方。因为这些都是二进制数,因此各比特为1较大功率的二比一到其右侧,位左移等于为每个被移位到位一次加倍的数目,等于由数乘以2 ^ x的。在你的榜样,寻找第四位,我们执行:

  1(2 ^ 0)<< (4-1)== 8(2 ^ 3)
00000001 LT;< (4-1)== 00001000

现在你知道它是如何做的,这是怎么回事在较低水平,为什么它的工作原理。

I have a byte, specifically one byte from a byte array which came in via UDP sent from another device. This byte stores the on/off state of 8 relays in the device.

How do I get the value of a specific bit in said byte? Ideally an extension method would look the most elegant and returning a bool would make the most sense to me.

public static bool GetBit(this byte b, int bitNumber)
{
    //black magic goes here
}

解决方案

Easy. Use a bitwise AND to compare your number with the value 2^bitNumber, which can be cheaply calculated by bit-shifting.

//your black magic
var bit = (b & (1 << bitNumber-1)) != 0;

EDIT: To add a little more detail because there are a lot of similar answers with no explanation:

A bitwise AND compares each number, bit-by-bit, using an AND join to produce a number that is the combination of bits where both the first bit and second bit in that place were set. Here's the logic matrix of AND logic in a "nibble" that shows the operation of a bitwise AND:

  0101
& 0011
  ----
  0001 //Only the last bit is set, because only the last bit of both summands were set

In your case, we compare the number you passed with a number that has only the bit you want to look for set. Let's say you're looking for the fourth bit:

  11010010
& 00001000
  --------
  00000000 //== 0, so the bit is not set

  11011010
& 00001000
  --------
  00001000 //!= 0, so the bit is set

Bit-shifting, to produce the number we want to compare against, is exactly what it sounds like: take the number, represented as a set of bits, and shift those bits left or right by a certain number of places. Because these are binary numbers and so each bit is one greater power-of-two than the one to its right, bit-shifting to the left is equivalent to doubling the number once for each place that is shifted, equivalent to multiplying the number by 2^x. In your example, looking for the fourth bit, we perform:

       1 (2^0) << (4-1) ==        8 (2^3)
00000001       << (4-1) == 00001000

Now you know how it's done, what's going on at the low level, and why it works.

这篇关于获得从字节的特定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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