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 |
Efficient data structure for store/search list of 1000 records a)array b)double linked list c)circular queue d)hash table
1) There is a singing competition for children going to be conducted at a local club. Parents have been asked to arrive at least an hour before and register their children’s names with the Program Manager. Whenever a participant registers, the Program Manager has to position the name of the person in a list in alphabet order. Write a program to help the Program Manager do this by placing the name in the right place each time the Program Manger enters a name. 2) the Event Manager has to send participants to the stage to perform in the order in which they registered. Write a program that will help the Event Manager know who to call to the stage to perform. The Logic should be in Data Structures
What is #include stdlib h?
Is c easier than java?
what is bit rate & baud rate? plz give wave forms
Is malloc memset faster than calloc?
code for inverse a matrix
Which is more efficient, a switch statement or an if else chain?
To what value do nonglobal variables default? 1) auto 2) register 3) static
What is the diffrent between while and do while statement ?
How can I implement opaque (abstract) data types in C? What's the difference between these two declarations? struct x1 { ... }; typedef struct { ... } x2;
diff. between *p and **p