How to make an array of bits? [duplicate] - c

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
}

Related

C - Replacing the nth byte of a 64 bit integer [duplicate]

This question already has answers here:
How do I set, clear, and toggle a single bit?
(27 answers)
Closed 2 years ago.
I'm trying to write a C function that takes a uint64_t and replace it's nth byte to a given one.
void setByte(uint64_t *bytes, uint8_t byte, pos)
I know I can easily get the nth byte like so
uint8_t getByte(uint64_t bytes, int pos)
{
return (bytes >> (8 * pos)) & 0xff;
}
But I have no idea how to Set the nth byte
Try this:
void setByte(uint64_t *bytes, uint8_t byte, int pos) {
*bytes &= ~((uint64_t)0xff << (8*pos)); // Clear the current byte
*bytes |= ((uint64_t)byte << (8*pos)); // Set the new byte
}
Use a mask to set every bit of the target byte to 0 (i.e. the mask should be all 1s but 0s at the target byte and then ANDed to the integer), then use another mask with all 0s but the intended value in the target byte and then OR it with the integer.

How to set n-th bit of *x to v value in C [duplicate]

This question already has answers here:
How do I set, clear, and toggle a single bit?
(27 answers)
Closed 5 years ago.
I need to implement this method.
I have the unsigned * x, I need to set v value in the n-th bit, using bitwise operators in C.
void set_bit(unsigned * x,
unsigned n,
unsigned v){
//I need the code here, thanks for your help!
}
Here's a macro to do that
#define SETBIT(var, bit) ((var) |= (1 << (bit)))

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.

Set the i-th bit to zero? [duplicate]

This question already has answers here:
How do I set, clear, and toggle a single bit?
(27 answers)
Closed 8 years ago.
I would like to set the i-th bit to zero no matter what the i-th bit is.
unsigned char pt = 0b01100001;
pt[0] = 0; // its not how we do this...
Setting it to one, we can use a mask pt | (1 << i) but i'm not sure how to create a mask for setting 0, if thats possible.
You just have to replace the logical OR with a logical AND operation. You would use the & operator for that:
pt = pt & ~(1 << i);
You have to invert your mask because logical ANDing with a 1 will maintain the bit while 0 will clear it... so you'd need to specify a 0 in the location that you want to clear. Specifically, doing 1 << i will give you a mask that is 000...010..000 where the 1 is in the bit position that you want, and inverting this will give 111...101...111. Logical ANDing with this will clear the bit that you want.
You could stick with this:
// Set bit at position `bitpos` in `pt` to `bitval`
unsigned char bitpos = 1;
unsigned char pt = 0b01100001;
bool bitval = 1;
// Clear the bit
pt &= ~(1u << bitpos);
// Set the bit
pt |= (bitval << bitpos);

C - Get a bit from a byte [duplicate]

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.

Resources