【问题标题】:How to access a specific bit given a byte in C?如何在 C 中访问给定字节的特定位?
【发布时间】:2014-03-27 02:54:38
【问题描述】:

例如,假设我有类似“0000 0000 0000 1110”的内容。

如何访问最左边的 1 并将其更改为 0?

【问题讨论】:

标签: c binary byte bits


【解决方案1】:

这两个函数可以处理 64 位值。

uint8_t get_bit(uint64_t bits, uint8_t pos)
{
   return (bits >> pos) & 0x01;
}

uint64_t set_bit(uint64_t bits, uint8_t pos, uint8_t value)
{
   uint64_t mask = 1LL << (63 - pos);
   if (value)
       bits |= mask;
   else
       bits &= ~mask;
   return bits;
}

uint64_t v = ...;
uint8_t i = 63;
for(;i>=0; i--)
{
    if(get_bit(v, i)){
       v=set_bit(v,i, 0);
       break;
    }
}

【讨论】:

  • 单线获取设置为 1 的 msb:i = floor(log(bits)/log(2))
【解决方案2】:

使用按位与 (&) 。像 0000 0000 0000 1110 & 0000 0000 0000 1000 会给出非零答案

【讨论】:

    【解决方案3】:

    我认为您正在寻找 bit masking

    例如:

    00011001 >> 3 = 00000011
    

    现在用 1 掩盖它

    00000011 & 00000001 = 00000001
    

    代码:-

    int funcBitMasking(int8_t mybyte, int firstbit)
    {
        if (firstbit> 0 && firstbit<= 16)
            return (mybyte & (1<<(firstbit-1)));
        else
            return 0;
    }
    

    【讨论】:

      【解决方案4】:

      我假设您的输入是 32 位。我们扫描输入的第一个 1,左移;通过&amp;0X7FFF 将第一位更改为 0,然后右移我们左移 + 1 的数字。

      unsigned int i = 0XE;
      int j;
      
      for ( j = 0; j < 32 && ! ( i & 0X8000 ); j++ )
          i <<= 1;
      
      i &= 0X7FFF;
      i >>= ( j + 1 );
      

      【讨论】:

        【解决方案5】:

        假设 a = 0000 0000 0000 1110

        int i =16 ;
         b = 1;
        while(true)
        {
        
          b = b << 1 ;
          if(b & 0x1000000000000000)
          {
           break;
          }
         i = i +1 ;
        }
        

        这样你就可以访问你的位了

        int yourbit= ~0; /* All 1’s */ 
        youtbit = (max >> (i+1) ) +  1
        

        【讨论】:

          【解决方案6】:

          我认为您将右侧的第四位称为最左侧的位。

          要访问特定位,您可以使用Shift Operators

          如果您要重置的始终是1,那么您可以使用&amp; 操作。

          但是,如果它也可以取0 值,那么&amp; 操作将失败为0 &amp; 1 = 0。在那段时间你可以使用| (OR)

          将此值存储在变量中并使用(1 &lt;&lt; 4) 进行OR 操作 Shift 运算符的右侧将从右侧获取位的位置。并且,Shift 运算符的左侧将采用要放置在该位置上的值。算子指向方向,值要移位。

          请参考以下链接,了解更多信息。

          http://www.eskimo.com/~scs/cclass/int/sx4ab.html

          http://www.codeproject.com/Articles/2247/An-introduction-to-bitwise-operators

          希望对您有所帮助。

          【讨论】:

            【解决方案7】:
            #include <stdio.h>
            #include <stdint.h>
            
            int mlb_pos(uint16_t x) {//0 origin
                uint16_t y;
                int n = 16;
                y = x >>  8; if (y != 0){ n = n -  8 ; x = y; }
                y = x >>  4; if (y != 0){ n = n -  4 ; x = y; }
                y = x >>  2; if (y != 0){ n = n -  2 ; x = y; }
                y = x >>  1; if (y != 0){ return 15-(n-2); }
                return 15-(n-x);
            }
            
            int main(){
                uint16_t n = 14;//0b0000000000001110
                uint16_t result = n ^ (1<<mlb_pos(n));
                printf("%d\n", result);//6 : 0b0000000000000110
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-09-28
              • 2021-03-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多