C - Get a bit from a byte [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to get bit by bit data from a integer value in c?
I have a 8-bit byte and I want to get a bit from this byte, like
getByte(0b01001100, 3) = 1

Firstoff, 0b prefix is not C but a GCC extension of C.
To get the value of the bit 3 of an uint8_t a, you can use this expression:
((a >> 3) & 0x01)
which would be evaluated to 1 if bit 3 is set and 0 if bit 3 is not set.

First of all C 0b01... doesn't have binary constants, try using hexadecimal ones. Second:
uint8_t byte;
printf("%d\n", byte & (1 << 2);

Use the & operator to mask to the bit you want and then shift it using >> as you like.

Related

How to make an array of bits? [duplicate]

This question already has answers here:
How to define and work with an array of bits in C?
(5 answers)
Closed 5 years ago.
I want to make an array of int bit fields where each int has one bit, meaning that all of the numbers will be 1 or 0, how can I code that?
I tried
struct bitarr {
int arr : 1[14];
};
but that doesn't compile and I don't think that this is the way
You can not do array of these bits. Instead, create single 16-bit variable for your bits, then instead of accessing it as i[myindex] you can access it as bitsVariable & (1 << myindex).
To set bit, you can use:
bitsVariable |= 1 << myindex;
To clear bit, you can use:
bitsVariable &= ~(1 << myIndex);
To check bit, you can use:
if (bitsVariable & (1 << myIndex)) {
//Bit is set
} else {
//Bit is not set
}

Fastest way to access a specific bit in a 2 dimensional bit array in C? [duplicate]

This question already has answers here:
C/C++ efficient bit array
(10 answers)
Closed 8 years ago.
I'm working with limited memory and need to quickly access a single bit in an array char s[80][10], which effectively gives me an 80x80 array with each index only being a single bit.
This is how I'm currently setting, clearing, and checking individual bits:
s[(row)][(col) >> 3] |= (0x80 >> ((col) & 0x07)); //set bit at s[row][col]
s[(row)][(col) >> 3] &= ~(0x80 >> ((col) & 0x07)); //clear bit at s[row][col]
int bit = (s[row][(col) >> 3] & (0x80 >> ((col) & 0x07)) ? 1 : 0); // read bit at s[row][col] into int bit
Is there some simplification to make these perform faster?
Thanks!
s[row][col] |= (1 << bit); // set bit
s[row][col] &= ~(1 << bit); // clear bit
(s[row][col] & (1 << bit))!=0 // read bit
This is the fastest possible as far as C goes. Further optimizations will rely on system-specific things and have to be done by the compiler.
As single operations, you can't really do much better. But within a larger algorithm, you could factor-out part of the addressing by using coherency.
That is, with a two dimensional array [M][N], accessing element [i][j] is the same as accessing [i*N+j] of the equivalent 1-dimensional array. That's a multiplication which can be factored out of an inner loop if it changes less often.

How can I assign bit by bit to an integer in C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you set, clear and toggle a single bit in C?
I want to create an assembler , so I need to assign 32 bits bit by bit or field by field to create 32 bit opcode from assembly ... how can I do this in C ? how can I assign bits in integer ? can this be done ?
You can declare these two macros to help you:
#define Set_Bit(IntValue, BitNumber) IntValue = IntValue | (1<<BitNumber)
#define Clr_Bit(IntValue, BitNumber) IntValue = IntValue & (~((1) << (BitNumber))))
Some questions have discussed these before:
Macros to set and clear bits

Get single bytes from a word [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the value of individual bytes of a variable?
How can I get single bytes from a word (for example an unsigned int in C) without using bit-wise operations (that is using arithmetic operations?)
I don't know why, but this formula (C-Like) for an x number doesn't seem to work:
floor(x / pow(R, i)) % R
where R is the radix with which the number is represented, and i is used to indicate the i-th byte to obtain.
If you really need to avoid bitwise operations, you can cheat alternatively (beware if you're using a little or big endian machine!):
char *int_16_storage;
uint16_t the_word = 0xabcd;
int_16_storage = &the_word;
uint8_t low_byte = int_16_storage[0];
uint8_t high_byte = int_16_storage[1];

How to work with individual bits? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Explanation of an algorithm to set, clear and test a single bit
I have an unsigned char. I would like to have bits 2 through 4 (counting from the least significant bit as 0) copied to another unsigned char as the first three bits. For example, in
abcdefgh // a,b,c,d,e,f,g,h are 0 or 1
becomes
00000def
I have tried
unsigned char input, output;
output = (input << 3) >> 5;
which does not work, but
output = (input << 3)
output >>= 5;
does work.
Is there a way in C to accomplish this in one line?
shift it, then mask the rest off:
output = ( input >> 2 ) & 0x07;
This gets only the bits you want then shifts them to the right. It's the opposite approach of #rsaxvc.
output = (input & 28) >> 2;
Try this:
unsigned char input, output;
input = 0x12abcdef;
output = ((input & 0x00fff000) >> 3) & 0x00000fff;
I don't think you can just shift back and forth on the same line and assume every time you shift the space is filled by zeroes, but this may be compiler dependent, if you do this you'll get the right thing guaranteed.
I'm assuming by "first three bits" you mean the 3 least significant bits, which is the leftmost or first 3 bits on little-endian systems.

Resources