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 |
what is the output of the following program? main() { int c[]={2,8,3,4,4,6,7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf("%d",*c); ++q; } for(j=0;j<5;j++) { printf("%d",*p); ++p; } }
Write a program to find the biggest number of three numbers in c?
How are structure passing and returning implemented?
what is the difference between call by value and call by reference?
5 Answers Genpact, Global Logic, Infosys,
Who had beaten up hooligan "CHAKULI" in his early college days?
du u know test pattern for robosoft? Plz share
1 Answers RoboSoft, TATA, Wipro,
What does %d do?
What are the properties of union in c?
How do you view the path?
Explain what are the different file extensions involved when programming in c?
What is atoi and atof in c?
Program to simulate second clock