【问题标题】:storing a bit in a bit of character array in C linux [closed]在C linux中的一个字符数组中存储一个位[关闭]
【发布时间】:2014-05-08 08:52:20
【问题描述】:

我怎样才能一点一点地存储一点? 我需要提取各种号码。来自不同变量的位并将它们放入缓冲区中,如图所示。

unsigned char a;
a=5;

现在我想获取 LSB 并将其存储在 unsigned char 类型的缓冲区中。

unsigned char buffer[5];

为了提取我正在使用

a & 00000001

现在如何存储它以及之后的更多位?

【问题讨论】:

  • 除非我错过了一些更深层次的错误或隐藏的含义:buffer[0] = a & 1 ..?? (另外,在 C 语言中输入以 0 开头的数字时,最好小心!)
  • buffer[0] 是一个字节,而不是位...
  • 然后使用buffer[0] = a。看?这样,您可以一次将 a 的所有“位”存储到 buffer[0] 中;您甚至不必一次这样做...请编辑您的问题并添加更多详细信息。您实际上需要做什么?如您所见,“我想存储位”有点宽泛。
  • C 中除了位域外没有位数据类型。为什么不只存储buffer[0] = abuffer[0] = a & 0x01

标签: c char bit


【解决方案1】:

我不确定你想做什么,但这里是一个按位移位的例子。

unsigned char b;
unsigned char c;

b = a & (1 << 0); /* Will store the least significant bit of a in b*/
c = a & (1 << 1); /* Will store the 2nd least significant bit of a in c*/

您可以使用它来创建只有有限位数的变量。

typedef struct bit_s
{
    unsigned int    a : 1; /* only 1 bit available */
    unsigned int    b : 12; /* only 12 bits available */
}              bit_t;

bit_t var;

var.a = 0;
var.a = 1;
var.a = 2; /* Will overflow the variable and create a warning with -Woverflow */

【讨论】:

    【解决方案2】:

    使用移位运算符&gt;&gt;

    buffer[0] = a & 0x01; // 1 will be stores to buffer[0] if LSB of a is one
    buffer[1] = (a >> 1) & 0x01; // 1 will be stored to buffer[1] if 2nd bit of a is one.
    

    【讨论】:

      【解决方案3】:

      类似:

      (apologies for syntax errors etc. this is untested code! ) 
      
      
      unsigned char getbit;
      unsigned char storeBit[5];
      
      for (x = 0; x < inBufferLength ; x++) {
        // get least significant bit
        getbit = inBuffer[x] & x00000001;
        int ByteNum = x / 8;
        int BitNum = x % 8;
        if (getBit) {
          switch (BitNum) {
           case 0;
              storeBit[ByteNum] = storeBit[ByteNum] | x10000000;
              break; 
           case 1;
              storeBit[ByteNum] = storeBit[ByteNum] | x01000000;
              break; 
           case 2;
              storeBit[ByteNum] = storeBit[ByteNum] | x00100000;
              break;
           case 3;
              storeBit[ByteNum] = storeBit[ByteNum] | x00010000;
              break; 
           case 4;
              storeBit[ByteNum] = storeBit[ByteNum] | x00001000;
              break; 
           case 5;
              storeBit[ByteNum] = storeBit[ByteNum] | x00000100;
              break; 
           case 6;
              storeBit[ByteNum] = storeBit[ByteNum] | x00000010;
              break; 
           case 7;
              storeBit[ByteNum] = storeBit[ByteNum] | x00000001;
              break;
           }
        }
      

      【讨论】:

        【解决方案4】:

        设置位...

        /** Set bit in any sized bit block.
         *
         * @return   none
         *
         * @param   bit    - Bit number.
         * @param   bitmap - Pointer to bitmap.
         *
         * @note    Please note that this function does not know the size of the
         *          bitmap and it cannot range check the specified bit number.
         */
        void SetBit( int bit, unsigned char *bitmap)
        {
            int n, x;
        
            x = bit / 8;                        // Index to byte.
            n = bit % 8;                        // Specific bit in byte.
        
            bitmap[x] |= (1 << n);      // Set bit.
        }
        

        重置位...

        /** Reset bit in any sized mask.
         *
         * @return  None
         *
         * @param   bit    - Bit number.
         * @param   bitmap - Pointer to bitmap.
         *
         * @note    Please note that this function does not know the size of the
         *          bitmap and it cannot range check the specified bit number.
         */
        void ResetBit( int bit, unsigned char *bitmap)
        {
            int n, x;
        
            x = bit / 8;                        // Index to byte.
            n = bit % 8;                        // Specific bit in byte.
        
            bitmap[x] &= (1 << n);              // Reset bit.
        }
        

        切换位...

        /** Toggle bit in any sized bit mask.
         *
         * @return   none
         *
         * @param   bit    - Bit number.
         * @param   bitmap - Pointer to bitmap.
         *
         * @note    Please note that this function does not know the size of the
         *          bitmap and it cannot range check the specified bit number.
         */
        void ToggleBit( int bit, unsigned char *bitmap)
        {
            int n, x;
        
            x = bit / 8;                        // Index to byte.
            n = bit % 8;                        // Specific bit in byte.
        
            bitmap[x] ^= (1<<n);        // Toggle bit.
        }
        

        检查位...

        /** Checks specified bit.
         *
         * @return  1 if bit set else 0.
         *
         * @param   bit    - Bit number.
         * @param   bitmap - Pointer to bitmap.
         *
         * @note    Please note that this function does not know the size of the
         *          bitmap and it cannot range check the specified bit number.
         */
        int IsBitSet( int bit, const unsigned char *bitmap)
        {
            int n, x;
        
            x = bit / 8;            // Index to byte.
            n = bit % 8;            // Specific bit in byte.
        
            // Test bit (logigal AND).
            if (bitmap[x] & (1<<n))
                return 1;
        
            return 0;
        }
        

        是位复位...

        /** Checks specified bit.
         *
         * @return  1 if bit reset else 0.
         *
         * @param   bit    - Bit number.
         * @param   bitmap - Pointer to bitmap.
         *
         * @note    Please note that this function does not know the size of the
         *          bitmap and it cannot range check the specified bit number.
         */
        int IsBitReset( int bit, const unsigned char *bitmap)
        {
            return IsBitSet( bit, bitmap) ^ 1;
        }
        

        希望这会有所帮助...

        Usefull bit twiddling hacks...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-01
          • 2020-12-19
          • 2020-05-26
          • 2022-10-09
          • 1970-01-01
          • 1970-01-01
          • 2023-03-25
          • 1970-01-01
          相关资源
          最近更新 更多