Question: in C Programming You have to write a program that will read a number followed by a series of bit operations from a file and
in C Programming
You have to write a program that will read a number followed by a series of bit operations from a file and perform the given operations sequentially on the number. The operations are as follows: set(x, n, v) sets the nth bit of the number x to v comp(x, n) sets the value of the nth bit of x to its complement (1 if 0 and 0 otherwise) get(x, n) returns the value of the nth bit of the number x The least significant bit (LSB) is considered to be index 0. Input format: Your program will take the file name as input. The first line in the file provides the value of the number x to be manipulated. This number should be considered an unsigned short. The following lines will contain the operations to manipulate the number. To simplify parsing, the format of the operations will always be the command name followed by 2 numbers, separated by tabs. For the set(x, n, v) command, the value of the second input number (v) will always be either 0 or 1. For the comp(x, n) and get(x, n) commands the value of the second input number will always be 0 and can be ignored. Note that the changes to x are cumulative, rather than each instruction operating independently on the original x. Output format: Your output for comp and set commands will be the resulting value of the number x after each operation, each on a new line. For get commands, the output should be the requested bits value. Example Execution: For example, a sample input file file1.txt contains the following (except the annotation comments): 5 # x = 5 get 0 0 # get(x, 0), ignoring second value (0) comp 0 0 # comp(x, 0), ignoring second value (0) set 1 1 # set(x, 1, 1) The result of the sample run is: $ ./first file1.txt 1 4 6
. You may not use arithmetic or logic equivalents to the bit tasks being asked for. C provides six operators for bit manipulation: & bitwise AND The bits in the result are set to 1 if the corresponding bits in the two operands are both 1. | bitwise inclusive OR The bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive OR The bits in the result are set to 1 if exactly one of the corresponding bits in the two operands is 1. > right shift Shifts the bits of the first operand right by the number of bits specified by the second operand. ~ complement All 0 bits are set to 1 and 1 bits are set to 0.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
