Question
Q7 Bitmasks 3 Points Bitmasks (or often just 'masks') are binary constants that we use to perform bitwise operations in order to produce desired changes
Q7 Bitmasks
3 Points
Bitmasks (or often just 'masks') are binary constants that we use to perform bitwise operations in order to produce desired changes in particular bits of a larger value.
For instance, bitmasks may be used when working with UNIX file permissions. In UNIX, every file has read, write and execute permissions for the User (the user owner of the file), the Group (the owner group of the file) and Other (everyone else on the system). These permissions are stored as 9 bits - from left to right, 3 bits for read, write, and execute for each of User, Group, and Other (from left to right: rwxrwxrwx). For example, 0b110100100 is the stored value for a file with user read and write permissions, and group and other read permissions. You can actually see these bits for all the files in a directory, by running the command "ls -l" in a terminal.
Now consider how you would change these permissions. If 'f' is a variable holding the file permissions of a file of interest in its least significant 9 bits, how would you change only particular permissions while leaving the other bits of 'f' unchanged?
Note: Because we have not specified the data type of 'f', you don't know how long the masks need to be. Constants are typically extended with zeros. Here the NOT operator can come in handy for generating leading 1's.
Q7.1 Turn Bits On
1 Point
Fill in the blank in this C command to turn on the read permission for Group and Other. Use a binary constant without leading zeros.
f = f \midf=f ____ ;
Incorrect
SubmitLast submitted on Feb 13 at 9:29 PM
Q7.2 Change Bits
1 Point
Fill in the blank in this C command to toggle the write permission for User and Other. Use a hexadecimal constant.
f = f ^ ____ ;
Submit
Q7.3 Turn Off Bits
1 Point
Fill in the blank in this C command to remove both the read and execute permissions for Group. Use a decimal constant, and bitwise operators (ONLY if necessary). Make sure your answer is as simple as possible.
Hint: If you have a solution that is a bitwise operator and a number, you can (and should) simplify it to just a number.
f = f & ____ ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started