我怎么能稍微存储一下? 我需要提取各种不。来自不同变量的位并将它们放入缓冲区中,如图所示。
unsigned char a;
a=5;
现在我想把LSB存储在一个unsigned char类型的缓冲区中。
unsigned char buffer[5];
提取我正在使用
a & 00000001
现在如何在此之后存储它以及更多位?
答案 0 :(得分:2)
我不确定你想做什么,但这里有一个按位移位的例子。
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 */
答案 1 :(得分:1)
使用移位运算符>>
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.
答案 2 :(得分:1)
类似的东西:
(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;
}
}
答案 3 :(得分:1)
设置位......
/** 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;
}
希望这会有所帮助......