Question

# The bitwise operators are very similar to the ones that you might remember from Java. They...

The bitwise operators are very similar to the ones that you might remember from Java. They are used a lot more frequently in C, though, because C is mostly chosen when you want to write low level programs.

A quick reminder:

& (bitwise AND). Takes two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

| (bitwise OR). Takes two numbers as operand and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR). Takes two numbers as operand and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

<< (left shift). Takes two numbers, left shifts the bits of first operand, the second operand decides the number of places to shift.

>> (right shift). Takes two numbers, right shifts the bits of first operand, the second operand decides the number of places to shift.

~ (bitwise NOT). Takes one number and inverts all bits of it.

Write a C program that takes a number from the command line, prints the number, and prints the number of bits in the number that are set to 0 and 1.

For example:

./myProgram 48

In 48, there are 30 bits set to 0.

In 48, there are 2 bits set to 1.

Here is the implementation of the above program. To add any additional features in code change it.

I am using number to binary convertor then I am counting 1's in it and for 0's I am doing 32-no_of(1's).

#include <stdio.h>
void decToBinary(int n)
{
int res = 0;
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
if(binaryNum[j] == 1) res++;
printf("%d\n",res);
printf("%d",32-res);
}

int main(){
int k;
scanf("%d", &k);
decToBinary(k);
return 0;
}