Question

In: Computer Science

Q6. Perform the following operations using bit masking. (a) Set the bit 3 of a byte...

Q6. Perform the following operations using bit masking.

(a)

Set the bit 3 of a byte data

(b)

Clear the bit 5 of a byte data

(c)

Toggle the bit 7 of a byte data

(d)

Check the bit 0 of a byte data

Solutions

Expert Solution

#include <iostream>
using namespace std;
int main()
{ //int is of 32 bit but we only see on its last 8 bit
int num,mask,pos,ans; //num for example take 86 in binary 01010110
num = 86; //mask is for masking and pos for position where we have to perform
cout<<"Number is "<<num<<endl;
/* Part 1*/
cout<<"------ part 1-------"<<endl;
pos = 3; // Q1 set bit 3 of byte data
mask = (1<<pos); // left shift 1 three times which make 00001000 = 8 in decimal
ans = num|mask; // 01010110 num
cout<<"ans :"<<ans<<endl; // 00001000 mask bit wise or
// 01011110 ans = 94
/* part 2 */
cout<<"------ part 2-------"<<endl;
pos = 5; // Q2 clear bit 5 of byte data
mask = (1<<(pos-1)); // mask = 00010000 = 16 in decimal
ans = num &(~mask); // ~mask = 11101111 bit wise not
cout<<"ans :"<<ans<<endl; // 01010110 num   
// 11101111 ~mask bit wise andoperation
// 01000110 ans = 70
/* part 3 */
cout<<"------ part 3-------"<<endl;
pos = 7; // Q3 toggle bit 7 of byte data
mask = (1<<pos); // mask = 10000000 = 64 in decimal
ans = num^mask; // 01010110 num   
// 10000000 mask bit wise xor operation
cout<<"ans :"<<ans<<endl; // 11010110 ans = 214
/* part 4 */   
cout<<"------ part 4-------"<<endl;
pos = 0; // Q4 check bit 0 of byte data
mask = (1<<pos); // mask = 00000001
ans = num&mask; // 01010110 num
if(ans == 1) // 00000001 mask bit wise and operation
cout<<"SET BIT AT POSITION"; // 00000000 ans = 0 if last bit is not set then our ans = 0
else // and if it is set then ans =1
cout<<"NOT SET";
cout<<endl;
return 0;
}
/* In above code you can generalize it by taking num and pos as a input so it gives answer all all other queries*/


Related Solutions

Let A be a three-bit unsigned number. Configure a seven-bit adder to perform the following operations....
Let A be a three-bit unsigned number. Configure a seven-bit adder to perform the following operations. You may shift A, shift the output, and set specific inputs or outputs to 1 or 0, as needed. You may also use NOT gates to invert A, if needed. Make sure you label each bit of your output, and that your output uses enough bits. For examples of how to draw and label the figures, see P6. a. W = 3A + 2...
Problem: Perform following operations in binary using 8-bit addition/subtraction/multiplication. 1. −80 + 42 2. −99 −...
Problem: Perform following operations in binary using 8-bit addition/subtraction/multiplication. 1. −80 + 42 2. −99 − 20 3. 60 − 70 4. −59 × 3 5. 52×−1
An 8-bit byte with binary value 11001101 is to be encoded using an even-parity Hamming code....
An 8-bit byte with binary value 11001101 is to be encoded using an even-parity Hamming code. What is the binary value after encoding? A bit stream 10101010 is transmitted using the standard CRC method. The divisor is 1011. Show the actual bit string transmitted. Suppose the second bit from the left is inverted during transmission. Show that how this error is detected by the receiver? What is the maximum size of the sender window and receiver windows for each of...
For Networks -> Please tell me about the following: Contrast byte stuffing with bit stuffing. What...
For Networks -> Please tell me about the following: Contrast byte stuffing with bit stuffing. What are the two types of error control? And which is used more? How do you correct transmission errors after detecting an error? Parity, checksum and CRCs are a type of? Why are hamming codes used?
Generate all substrings of set {1, 2, 3} using the bit strings method. Show details of...
Generate all substrings of set {1, 2, 3} using the bit strings method. Show details of your work.
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
C++ 14.11 Lab # Map Write a menu driven program to perform following operations using a...
C++ 14.11 Lab # Map Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input...
Using pseudocode, write the code that will perform the necessary file operations as described in the...
Using pseudocode, write the code that will perform the necessary file operations as described in the short scenario below: “During registration at a college, student’s details will be captured by a staff member and written to a file called “studentDetails.dat”. The following student details will be captured: name, surname, studentNumber, registeredCourse. The staff member will be prompted for the student details. After every record added to the file, the staff member will be asked whether they would like to add...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT