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
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.
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 }
2. Consider a data set {3, 20, 35, 62, 80}, perform hierarchical clustering using complete linkage...
2. Consider a data set {3, 20, 35, 62, 80}, perform hierarchical clustering using complete linkage and plot the dendogram to visualize it. R code needed with full steps including packages
Perform each task, given the following set of data. 3 6 11 13 19 23 (a)...
Perform each task, given the following set of data. 3 6 11 13 19 23 (a) Viewing the data as a sample drawn from a larger population, find the sample standard deviation, s, of the data. (Round your answer to two decimal places.) s = (b) Viewing the data as an entire population, find the population standard deviation, σ, of the data. (Round your answer to two decimal places.) σ = (c) How do the answers to parts (a) and...
SOLVE FOLLOWING a.   Desgin and VERILOG code of a 3 bit up down counter USING T...
SOLVE FOLLOWING a.   Desgin and VERILOG code of a 3 bit up down counter USING T FLIP FLOP..... b. using behavioural module.Write a verilog discription of an N-BIT up down binary counter. Record the simulation output waveform in observation.....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT