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 volatile

2 Answers  


write a program to convert a expression in polish notation (postfix) to inline (normal) something like make 723+* (2+3) x 7 (not sure) just check out its mainly printing expression in postfix form to infix

0 Answers  


What is hashing in c language?

0 Answers  


Write a C program to convert an integer into a binary string?

1 Answers  


write a c program to find the largest and 2nd largest numbers from the given n numbers without using arrays

0 Answers   IBM,






You are given a string which contains some special characters. You also have set of special characters. You are given other string (call it as pattern string). Your job is to write a program to replace each special characters in given string by pattern string. You are not allowed to create new resulting string. You need to allocate some new memory to given existing string but constraint is you can only allocate memory one time. Allocate memory exactly what you need not more not less.

2 Answers   Microsoft,


Convert the following expression to postfix and prefix (A+B) * (D-C)

3 Answers   Satyam,


Explain what is wrong with this program statement?

0 Answers  


Is it possible to have a function as a parameter in another function?

0 Answers  


can we execute the program with the object file

1 Answers  


Why we use stdio h in c?

0 Answers  


what is the difference between these initializations? Char a[]=”string”; Char *p=”literal”; Does *p++ increment p, or what it points to?

4 Answers  


Categories