how to set Nth bit of variable by using MACRO
Answers were Sorted based on User's Feedback
Answer / kirankumaryakkala
#define set(val,n) val|=1<<n //do left-shift then bitwise OR
logic is val|=1<<n
means,
first shift the value 1 to ntimes and do the bitwise or with
value.
u will get the answer
| Is This Answer Correct ? | 30 Yes | 6 No |
Answer / vikram
The above code is wrong it should be n-1 instead of n.
#include<stdio.h>
#define SET(val,n) (val|=1<<(n-1))
main()
{
int n = 256;
printf("%d",SET(n,1));
}
| Is This Answer Correct ? | 13 Yes | 8 No |
Answer / sunitha
/* macro to set Nth bit */
#define SET_N_BIT(x,n) x|((~(unsigned)0)>>(8-(n-n-1))<<n);
Try out this . this is optimised version for setting a bit
work for any bit upto 8 bits if u want for 32 bits than
replace 8 with 32.
| Is This Answer Correct ? | 3 Yes | 0 No |
Average of a couple 10 years ago was 25. The average remains same after having a child and twins after 3 years. What is the present age of the first child
On most computers additional memory that is accessed through an adapter of feature card along with a device driver program. a) user memory b) conventional memory c) expandedmemory d) area
What will be your course of action for a push operation?
What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25
What is a buffer in c?
What is ctrl c called?
cavium networks written test pattern ..
Why is conio.h not required when we save a file as .c and use clrscr() or getch() ?
Is c dynamically typed?
what is function pointer?
What is the difference between exit() and _exit() function?
Explain what is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?