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 |
Why is this loop always executing once?
How many ways are there to swap two numbers without using temporary variable? Give the each logic.
You have an int array with n elements and a structure with three int members. ie struct No { unsigned int no1; unsigned int no2; unsigned int no3; }; Point1.Lets say 1 byte in the array element is represented like this - 1st 3 bits from LSB is one number, next 2 bits are 2nd no and last 3 bits are 3rd no. Now write a function, struct No* ExtractNos(unsigned int *, int count) which extracts each byte from array and converts LSByte in the order mentioned in point1.and save it the structure no1, no2, no3. in the function struct No* ExtractNos(unsigned int *, int count), first parameter points to the base address of array and second parameter says the no of elements in the array. For example: if your array LSB is Hex F7 then result no1 = 7, no2 = 2, no3 = 7. In the same way convert all the elements from the array and save the result in array of structure.
What is a dynamic array in c?
Is there a way to compare two structure variables?
What is indirect recursion? give an example?
Find the O/p of the following 1) #include int main() { char c='1'; int j=atoi(c); }
void main(int argc,char *argv[],char *env[]) { int i; for(i=1;i<argc;i++) printf("%s",env[i]); }
Why doesnt this code work?
can we change the default calling convention in c if yes than how.........?
Given an array of 1s and 0s arrange the 1s together and 0s together in a single scan of the array. Optimize the boundary conditions?
What is size of union in c?