Question

Create a program to ask the user for an integer number, positive only. If the number...

Create a program to ask the user for an integer number, positive only. If the number is between 0 and 255, printout the number in binary format to the screen. This will involve checking the bits, one by one, starting from the most significant place: 128.

Use c++

1. Modify the program to find the binary number, using a FOR loop, instead of the manual checking of each bit separately

2. What are the bitwise XOR and INVERSION operators? Find them and create a program that uses them. For the XOR, ask the user for a number, and XOR it with a hardcoded binary constant like 0b01010101. For the inversion, ask the user for a number, and print the result of its bitwise inversion, in both decimal and binary

Homework Answers

Answer #1

Ans 1. Using a For Loop

#include <iostream>
using namespace std;
void DecimalToBinary(int n)
{
int binaryNumber[100], num=n;
int i = 0;
while (n > 0) {
binaryNumber[i] = n % 2;
n = n / 2;
i++;
}
cout<<"Binary form of "<<num<<" is ";
for (int j = i - 1; j >= 0; j--)
cout << binaryNumber[j];
}
int main() {
int a;
cout<<"Enter the Number bw 0 to 255 : ";
cin>>a;
if(a >= 0 && a <= 255)
DecimalToBinary(a);
return 0;
}

Ans 2.  

Bitwise XOR (^): Before Bitwise XOR we need to know about XOR operation. Here is the truth table of XOR operation

a b a ^ b
0 0 0
0 1 1
1 0 1
1 1 0

In Bitwise XOR we perform XOR operation of every bit.

ex. 1 0 0 1 0 0

1 1 1 0 1 0

0 1 1 1 1 0

Bitwise Inversion(~): In Bitwise Inversion we invert every bit of the given binary number.

Ex a = 5 = 101

After Bitwise Inversion ~a will be -6.

~(0 0 0 0 0 1 0 1) = 1 1 1 1 1 0 1 0 = - 6

Bitwise XOR Code:

#include <iostream>
using namespace std;

int main()
{
int num1,num2;

cout<<"Enter the First Number = ";
cin>>num1;
cout<<"Enter the Second Number = ";
cin>>num2;
cout<<"Bitwise XOR of these Numbers( num1 ^ num2 ) " << (num1 ^ num2)<<endl;

return 0;
}

Bitwise Inversion Code

#include <iostream>
using namespace std;

void DecimalToBinary(int n)
{
int binaryNumber[100], num=n;
int i = 0;
while (n > 0) {
binaryNumber[i] = n % 2;
n = n / 2;
i++;
}
cout<<"Binary form of Inversion Number "<<num<<" is ";
for (int j = i - 1; j >= 0; j--)
cout << binaryNumber[j];
}

int main()
{
int num1;

cout<<"Enter the Number = ";
cin>>num1;
  
cout<<"Bitwise Inversion of the Number in Decimal = " << ~(num1)<<endl;
DecimalToBinary(~num1);

return 0;
}

Answer #2

e3fwsdfrewq

answered by: Roohi
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
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
Using C++ 1. Create a program that asks the user to create 5 triangles, asking for...
Using C++ 1. Create a program that asks the user to create 5 triangles, asking for 5 bases and 5 heights (you can use an array), have a function to print the triangle bases, heights, and areas 2. Create a structure for the Triangle that holds the base and height of the triangles. Ask the user for 5 triangles, and print them. 3. Create an array of 5 structures - ask the user for 5 triangles, and then print the...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Create a program that prompts the user for an integer number and searches for it within...
Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? in java
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
Write a mips assembly language program that asks the user to enter and integer number and...
Write a mips assembly language program that asks the user to enter and integer number and read it. Then ask him to enter a bit position (between 0 and 31) and display the value of that bit.
C++ Create a program that tells the user if the integer is a prime number or...
C++ Create a program that tells the user if the integer is a prime number or not. Please do not use flag or bool.
create a basic python program that converts binary to number (using if statements etc) Asking user...
create a basic python program that converts binary to number (using if statements etc) Asking user for input of binary number converting/calculating printing