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.

Your assignment:

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

Your number was 48.

In 48, there are 30 bits set to 0.

In 48, there are 2 bits set to 1.

Homework Answers

Answer #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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
Adder Start out by picking 2 positive six bit binary numbers that are less than 3210,...
Adder Start out by picking 2 positive six bit binary numbers that are less than 3210, written in 2's complement notation. The eventual goal is to add these two numbers. 1) Look at the LSB bit of the numbers, and using logic gates (NANDs, NORs, etc.) design a circuit that correctly gives the right output for any possible combination of bits in the LSB place. 2) Now look at the next column to the left (next to LSB). In this...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...
Write a sequence of two instructions that copies bits 0-5 from AL to bits 0-5 in...
Write a sequence of two instructions that copies bits 0-5 from AL to bits 0-5 in BL. Bits 6-7 in BL should be cleared, and AL should be unchanged             Mov al, bl       And 00111111, bl       Write a sequence of two instructions that copies the integer in bits 4-7 from AL register into bits 0-3 of the BL register. Upper 4 bits of AL and BL will be cleared             Shr al, 4       Mov bl,...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret code so that only authorised entities can read it. Encrypting data is considered a very effective way of achieving data security. To access encrypted data, you must have access to a secret key that enables you to decrypt it. Unencrypted data is called plain text; encrypted data is referred to as cipher text. There are two types of encryption: • Symmetric encryption • Asymmetric...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion to solve various problems. Some problems will have fairly short solutions, but declaring and using classes/objects is still required. Some could benefit from having some kind of data structure being passed between calls. Feel free to use your LinkedList if you think it could help. Some solutions will be a mix of iteration and recursion, but all solution need to be recursive in some...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...