You are on page 1of 5

1 How to set a particular bit?

Ans Setting a Bits

Bitwise OR operator (|) use to set a bit of integral data type.”OR” of two bits is always one
if any one of them is one.

2 How to check if a particular bit is set?

ANS To check the nth bit, shift the ‘1’ nth position toward the left and then “AND” it with
the number.

3 How to toggle a particular bit?

Ans Bitwise XOR (^) operator use to toggle the bit of an integral data type. To toggle the
nth bit shift the ‘1’ nth position toward the left and “XOR” it.

4  How to Rotate bits of a number?

Ans There are two types of rotation possible left and right. In the left rotation, the bits that
fall off at the left end are put back at the right end and in the right rotation, the bits that
fall off at the right end are put back at the left end. 

5 Clear all bits from MSB to the ith bit

Ans Here I have supposed data is stored using 8 bits.

let’s assume the ith position is 2.

mask =(1 <<( i+1)); // give you 00001000

so now if we subtract 1 from the mask (mask = mask – 1), then we will get 00000111

Using the mask, now we can clear MSB to ith bits of data (15).

data = data & mask; // Now bits are clear


6 Multiply a number by 2 using bitwise operation

Ans Left shifting of a data (number) by 1 is equivalent to data*2. In data, every bit is a
power of 2, with each shift we are increasing the value of each bit by a factor of 2.

#include <stdio.h>
int main()
{
unsigned int data = 15; //value of data
data = data << 1; // equivalent to data * 2
printf("data = %d\n", data);
return 0;
}

7 Multiply a given Integer with 3.5 using bitwise operation

Ans  multiplication is basically an addition, so we can multiply a given integer (data) with
3.5 using the following operation, (2 *data) + data + (data/2).

#include <stdio.h>
int main()
{
unsigned int data = 10; //value of data
data = (data<<1) + data + (data>>1);; // equivalent to data * 3.5
printf("data = %d\n", data);
return 0;
}

8 Swap two nibbles of a byte

Ans A nibble consists of four bits, sometime interviewer asked the question to swap the
nibble of a byte. It is a very easy question, here << (left shift) and >> (right shift) operators
are used to swap the nibble.

#include <stdio.h>
//Macro to swap nibbles
#define SWAP_NIBBLES(data) ((data & 0x0F)<<4 | (data & 0xF0)>>4)
int main()
{
unsigned char value = 0x23; //value in hex
printf("0x%x", SWAP_NIBBLES(value)); //print after swapping
return 0;
}

9 How do I get a bit from an integer value in C?

Ans To get the ith bit, perform Anding operation between the ith bit and 1 (1 << i) after
that shift the result ith position in right using the right operation.

10 Write MACRO to swap the bytes in 32bit Integer Variable.

Ans #include <stdio.h>


#include <inttypes.h>
#define SWAP_BYTES(u32Value) ((u32Value & 0x000000FF) << 24)\
|((u32Value & 0x0000FF00) << 8) \
|((u32Value & 0x00FF0000) >> 8) \
|((u32Value & 0xFF000000) >> 24)
int main()
{
uint32_t u32CheckData = 0x11223344;
uint32_t u32Result = 0;
u32Result = SWAP_BYTES(u32CheckData); //swap the data
printf("0x%x\n",u32Result);
return 0;
}

11Count number of bits to be flipped to convert A to B

Suppose, A = 8, B = 7
Binary representation of A => 00001000
Binary representation of B => 00000111
Here we have to flip highlighted four bits of A
to make it B.

12 Is it legal to initialize an array int i[] = {1, 2, 3, 4, 5};


Ans Yes, it's perfectly legal. You can create and initialize an array in the same line

13 Can you declare an array without assigning the size of an array?

Ans No we cannot declare an array without assigning size.

If we declare an array without size, it will throw compile time error

Example: marks = new int []; //COMPILER ERROR

14 What is the default value of Array?

Ans Any new Array is always initialized with a default value as follows

 For byte, short, int, long – default value is zero (0).


 For float, double – default value is 0.0.
 For Boolean – default value is false.
 For object – default value is null.

15  Can we declare array size as a negative number?

Ans No. We cannot declare the negative integer as an array size.

If we declare, there will be no compile-time error.

However, we will get NegativeArraySizeException at run time.

16  When will we get ArrayStoreException?


Ans It is a runtime exception. For example, we can store only string elements in a String
Array. If anybody tries to insert integer element in this String Array, then we will get
ArrayStoreException at run time.

17 Is there any difference between int[] a and int a[]?

Ans No difference both are the legal statement.

18 int a[] = new int[3]{1, 2, 3} – is it a right way to declare arrays 

Ans No. We should not mention the size of the array when we are providing the array
elements.

19 How do we search a specific element in an array?

Ans We can use Arrays.binarySearch() method. This method uses binary search algorithm.

20 How do we find duplicate elements in an array?

Ans Using Brute Force Method:

In this method, we compare each element of an array with other elements.

If any two elements found equal or same, we declare them as duplicates. 

You might also like