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

How to declare a variable?

0 Answers  


input any 4 digit number and find the difference of all the digits?

3 Answers   Google,


What are c header files?

0 Answers  


character array A[12] can hold

5 Answers   Wipro,


Why ordinary variable store only one value  

0 Answers  






What is a far pointer in c?

0 Answers  


typedef struct { int i:8; char c:9; float f:20; }st_temp; int getdata(st_temp *stptr) { stptr->i = 99; return stptr->i; } main() { st_temp local; int i; local.c = 'v'; local.i = 9; local.f = 23.65; printf(" %d %c %f",local.i,local.c,local.f); i = getdata(&local); printf("\n %d",i); getch(); } why there there is an error during compiling the above program?

1 Answers  


x=2,y=6,z=6 x=y==z; printf(%d",x)

13 Answers   Bharat, Cisco, HCL, TCS,


how to use enum datatype?Please explain me?

3 Answers   Excel,


Explain what happens if you free a pointer twice?

0 Answers  


in linking some of os executables are linking name some of them

0 Answers   IBM,


What is a void pointer in c?

0 Answers  


Categories