Question

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.

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;

}

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, 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 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...

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 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 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. 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 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...

Microprocessor 8086 material.
Choose the correct answer.
1- For a (64K x 8) RAM, the number of address lines is:
*
17
19
16
15
20
14
18
2-Which of the following instructions requires (BYTE PTR)?
*
SHR [500H], 3h
LDS SI, [1000H]
LAHF
PUSH [9AAH]
ADD AX, [800H]
3-Where are the interrupt vectors located in the
microprocessor’s memory? *
in the first 64K byte
in the first 1K byte
in the first 256K byte
in the first 1M byte...

Strings
The example program below, with a few notes following, shows how
strings work in C++.
Example 1:
#include <iostream> using namespace std; int main()
{
string s="eggplant";
string t="okra";
cout<<s[2]<<endl;
cout<< s.length()<<endl; //prints 8
cout<<s.substr(1,4)<<endl; //prints ggpl...kind of
like a slice, but the second num is the length of the
piece
cout<<s+t<<endl; //concatenates: prints eggplantokra
cout<<s+"a"<<endl;
cout<<s.append("a")<<endl; //prints eggplanta: see
Note 1 below
//cout<<s.append(t[1])<<endl; //an error; see
Note 1
cout<<s.append(t.substr(1,1))<<endl; //prints
eggplantak; see Note 1
cout<<s.find("gg")<<endl;
if (s.find("gg")!=-1)
cout<<"found...

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 9 minutes ago

asked 37 minutes ago

asked 37 minutes ago

asked 37 minutes ago

asked 46 minutes ago

asked 52 minutes ago

asked 55 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago