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

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.
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
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.....
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...
Q6.      By using the stander normal curve, find the following. i.P ( -1.5 < z <...
Q6.      By using the stander normal curve, find the following. i.P ( -1.5 < z < -1) ii.P ( z = 3) iii.P ( -2 < z < 3) iv.P(z < z0 ) = 0.125 v.Probability of z>-1 or z<2
Using a spreadsheet (such as Excel) or a calculator, perform the operations described below. Document results...
Using a spreadsheet (such as Excel) or a calculator, perform the operations described below. Document results of all intermediate modular multiplications. Determine a number of modular multiplications per each major transformation (such as encryption, decryption, primality testing, etc.). a. Test all odd numbers in the range from 223 to 229 for primality using the Miller-Rabin test with base 2. b. Encrypt the message block M= 7 using RSA with the following parameters: e = 17 and n = 223 *229....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT