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

to find the program of matrix multiplication using arrays

6 Answers   Bhel,


what is ram?

3 Answers   TCS,


Go through this linked list concept.While traversing through the singly linked list sometimes the following code snippet "while(head != NULL)" is used and other times "while(head->link != NULL)"is used(Here head is the pointer pointing to the first node,node has two parts data part and link part).What is the difference between head != NULL and Head->link != NULL and in which situation are they used?

1 Answers   Oracle,


What is the difference between procedural and declarative language?

0 Answers  


What are the types of type specifiers?

0 Answers  






write a program in c language for the multiplication of two matrices using pointers?

8 Answers   Ignou,


wat is the difference between a definition and declaration? float y;---it looks like a declaration..but it s a definition.how?someone explain

3 Answers   TCS,


What are derived data types in c?

0 Answers  


What is a null pointer in c?

0 Answers  


Explain the bubble sort algorithm.

0 Answers  


Why main is not a keyword in c?

0 Answers  


write a program that declares an array of 30 elements named "income" in the main functions. then cal and pass the array to a programmer-defined function named "getIncome" within the "getIncome" function, ask the user for annual income of 30 employees. then calculate and print total income on the screen using the following function: "void getIncome ( ai []);

0 Answers   TCS,


Categories