[ACCEPTED]-Bit operations in C-bit-manipulation
//turn on isUsed
data |= 1;
//turn off isUsed
data &= ~1;
//turn on notLast
data &= ~2;
//turn off notLast
data |= 2;
0
This is very simple:
/* Turn on bit 0 */
code = code | 1;
/* Turn off bit 0 */
code = code & ~1;
/* Turn on bit 1 */
code = code | 2;
/* Turn off bit 1 */
code = code & ~2;
See Bitwise operators in C, or Google for 2 the appropriate terms. You can find this 1 in any book or tutorial about C.
In general, counting the least significant 5 bit as 0, to set bit N, you need to OR the 4 original value with 1 << N.
Eg to 3 set bit 1:
val |= (1 << 1);
To clear bit N, you need to AND 2 the original value with the bit-wise inverse 1 of 1 << N.
Eg to clear bit 1:
val &= ~(1 << 1);
This is begging for an interface, either 15 with functions or macros, something like:
// Use unsigned ints (assuming that's your 32-bit type).
#define setLast(x) (x) |= 2
#define clrLast(x) (x) &= ~2
#define isLast(x) ((x) & 2)
#define setUsed(x) (x) |= 1
#define clrused(x) (x) &= ~1
#define isUsed(x) ((x) & 1)
You 14 can also provide macros to extract the size 13 portion and create the whole integer:
#define getSize(x) ((x) >> 4)
#define create (sz,last,used) \
(((sz) & 0x0fffffff) << 4) | \
(((last) & 1) << 1) | \
(((used) & 1))
You'll 12 find your code becomes a lot more readable 11 if you provide the "functions" to do the 10 work and give them sensible names like the 9 above. Otherwise your code is peppered with 8 bit manipulation instructions that are harder 7 to understand.
Just keep in mind the normal 6 rules for macros, things like not passing 5 in things like x++
if your macros use it more 4 than once (which isn't actually the case 3 here). If you want to be ultra-safe, you 2 can do them as functions.
Equivalent functions 1 would be:
unsigned int setLast (unsigned int *x) { *x |= 2; return *x; }
unsigned int clrLast (unsigned int *x) { *x &= ~2; return *x; }
unsigned int isLast (unsigned int x) { return x & 2; }
unsigned int setUsed (unsigned int *x) { *x |= 1; return *x; }
unsigned int clrUsed (unsigned int *x) { *x &= ~1; return *x; }
unsigned int isUsed (unsigned int x) { return x & 1; }
unsigned int getSize (insigned int x) { return x >> 4; }
unsigned int create (unsigned int sz, unsigned int last, unsigned int used) {
unsigned int ret =
((sz & 0x0fffffff) << 4) |
((last & 1) << 1) |
((used & 1));
return ret;
}
Turn the flag on:
register |= (1<<LAST_BIT);
Turn the flag off:
register &= ~(1<<LAST_BIT);
Another 1 way is to use union bit-fields:
union
{
uint32_t value;
struct
{
unit32_t body:28;
unit32_t reserved:2;
unit32_t last_bit:1;
unit32_t used_bit:1;
} fields;
} MyResister;
MyResister.fields.last_bit = 1;
MyResister.fields.used_bit = 0;
I would throw in a BIT(x) macro just to 12 make the source code more clear:
#define BIT(n) (0x1U << (n))
Which would 11 result in:
#define LAST_SET(x) ((x) |= BIT(1))
#define LAST_CLR(x) ((x) &= ~BIT(1))
Also, as previously noted, always put 10 the parameter in parenthesis.
(OT) Edit: Changed 9 name of macro as I do not like having the 8 verb first. First of all a function like 7 getWhatever is for code where you can group 6 the function in a class. In C, IMHO, you 5 should put the "component" name first such 4 as, timeGet() et c
(OT2) Also if it's a register 3 macrofication like this is nice which would 2 result in better portability:
#define MY_REG_RD() (MY_REG)
#define MY_REG_WR(x) (MY_REG = (x))
#define MY_REG_SET(x) (MY_REG |= (x))
#define MY_REG_CLR(x) (MY_REG &= ~(x))
#define MY_REG_DIS BIT(10)
#define MY_REG_EN BIT(4)
Then you could 1 do:
MY_REG_SET(MY_REG_EN);
bool isBitOn( int mask , int i ){ // returns True if i-Th bit is On
return mask & ( 1 << i ) ;
}
int BitOn( int mask , int i ){ // Turn On the i-Th bit of the value and then returns it
return mask | ( 1 << i ) ;
}
int BitOff( int mask , int i ){ // Turn Off the i-Th bit of the value and then returns it
return mask - ( 1 << i ) ;
}
int BitToggle( int mask , int i ){ // Toggle the i-Th bit of the value and then returns it
return mask ^ ( 1 << i ) ;
}
void printbit(int n) { // print the Binary representation of a Integer Number
for(int i = 31 ; i >=0 ; i-- )
printf("%d", isBitOn(n,i) );
printf("\n");
}
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.