### Interview questions on Bitwise Operators

1)Which bit wise operator is suitable for turning off a particular bit in a number?

2)what will be printed out when the following code is executed:

main()
{
printf("%x",-1<<4);
}

3)Which one is equivalent to multiplying by 2?

* Left shifting a number by 1
* Left shifting an unsigned int or char by 1?

4)Write a function which gets the n bits from an unsigned integer x, starting from position p .(the right most digit is at position 0)

5)Write a function using bitwise opetators to check whether an integer is a power of 2 or not?

6)Write a Program that swaps the contents of two variables without
using any other variable,using bitwise operators?

7) Which bit wise operator is suitable for checking whether a particular bit is on or off?

8) Which bit wise operator is suitable for putting on a particular bit in a number?

9) Which bit wise operator is suitable for checking whether a particular bit is on or off?

10)Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y,leaving the other bits changed.

11)Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted leaving other unchanged.

#include "stdafx.h"

#include "stdio.h"
void SetNthBit(int *number, int pos)
{
int i;
int x = 1;

x = x << (pos-1);

*number = *number | x;
}

void ResetNthBit(int *number, int pos)
{
int i;
int x = 1;

//First get the Nth bit value to know whether it is 1 or 0

int temp;

temp = (*number) >>(pos-1);
if( (temp % 2) == 0)
{
//The Nth bit is 0. Hence it is already Reset
}
else
{
x = x << (pos-1);
*number = (*number) ^ x;
}
}
int main(int argc, char* argv[])
{
int number = 2;
printf("Number before setting the bit %d\n",number);
// Set the 3rd bit to 1 from Right most bit
SetNthBit(&number,3);
printf("Number after setting the bit %d\n",number);
ResetNthBit(&number,3);
printf("Number after Re-setting the bit %d\n",number);
return 0;
}

2. find the no. of 1's in a given number using bitwise operations

3. main()

{
//Turn off a particular bit
int n = 4;
int x = 3; // Switch off 3rd bit
int i = 1 << x - 1;
i = ~i;
printf("%d", n & i);

//Test if << 1 time multiplies the number by 2
//Doesnt work for char
unsigned int b = 2147;
char c = 2147;
int d = 2147;

printf ("\nUnsigned %d",b << 1);
printf ("\nchar %d",c << 1);
printf ("\nint %d",d << 1);

//Swap
int x = 202;
int y = 409;
x = x ^ y;
y = x ^ y;
x = x ^ y;

printf("\nswapped %d %d",x,y);

}

//: create a mask where everywhere is 1 except that position and AND the number with the mask
int turn_off(int number, int pos) {
int mask = ~(1 << pos-1);
}

6. 1.OR
2.FFF0
3.LEFT SHIFTING AN UNSIGNED INT OR CHAR BY 1
4.GETTING X AND POSITION P
R1=X>>P;
TEMP=~R1;
TEMP=R1^X;
R1=X&TEMP;

5.GET X AS INPUT
FOR(I=0;I>I&10?PRINTF("YES"):PRINTF("NO");

6.GET A AND B ARE INPUTS
A=(A^B)^B;
B=(B^A)^A;

7.&
8.|
9.&
10.DONT KNOW
11.GET X ,POS ,N
WHILE(N>0)
{
IF(((X>>POS+1-N)&01)==0)
X=X|(01>>POS+1-N);
ELSE
{
TEMP=~X;
TEMP=TEMP|01;
TEMP=X^TEMP;
X=TEMP&X;
}
N--;
}

SAMPLE INPUT:
10101010
N=4;P=3;
OUTPUT: 1010 0101

7. 5. To check if it is a power of 2,

If the given number is n,

if n&(n-1) == o , power of 2 else not a power of 2

8. 1.and operator
2. for 16 bits ans is ffff for 32 bits ans is ffffffff
3. left shift the number by 1
4.

9. 11)
assuming rightmost digit is at pos 0
x = x XOR (pow(2, p+n) - pow(2,p))

10. Question #2 is implementation dependent. How the computer stores a signed number is not defined in the C spec and can change from compiler to compiler.

12. Q10.
void trunc(byte& x, byte p, byte n, byte y){
printf("y = %x\n",y);
y = y>>p;
byte t = 0xffffffff;
t = ~(t<<n);
printf("t = %x\n",t);
x = y & t;
printf("x = %x\n",x);
}

13. Q11.
void invert(byte& x, byte y, byte p, byte n){
printf("y = %x\n", y);
byte y_ = 0;
trunc(y_, p, n, y);

for (int i=0; i>= 1;
if (i!=0) x<<= 1;
}
printf("x = %x\n", x);
}

14. void invert(byte& x, byte y, byte p, byte n){
printf("y = %x\n", y);