how to set Nth bit of variable by using MACRO

Answers were Sorted based on User's Feedback



how to set Nth bit of variable by using MACRO..

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

how to set Nth bit of variable by using MACRO..

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

how to set Nth bit of variable by using MACRO..

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

Post New Answer

More C Interview Questions

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; } }

4 Answers  


Write a program to find the biggest number of three numbers in c?

0 Answers  


How are structure passing and returning implemented?

0 Answers  


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?

1 Answers  






du u know test pattern for robosoft? Plz share

1 Answers   RoboSoft, TATA, Wipro,


What does %d do?

0 Answers  


What are the properties of union in c?

0 Answers  


How do you view the path?

0 Answers  


Explain what are the different file extensions involved when programming in c?

0 Answers  


What is atoi and atof in c?

0 Answers  


Program to simulate second clock

2 Answers  


Categories