Question: Computer Organization and Architecture [Bitwise Operators] Please complete bit_ops.c by implementing the bit manipulation functions get_bit, set_bit, and flip_bit. The declarations of these functions are

Computer Organization and Architecture

[Bitwise Operators] Please complete bit_ops.c by implementing the bit manipulation functions get_bit, set_bit, and flip_bit. The declarations of these functions are giving below. You may only use bitwise operations such as and (&), or (|), xor (^), not (~), left shifts (>). You may not use any for/while loops or conditional statements.

bit_ops.c file: Note the //Your Code Here comments

#include  // Return the nth bit of x. // Assume 0 <= n <= 31 unsigned get_bit(unsigned x, unsigned n) { // YOUR CODE HERE // Returning -1 is a placeholder (it makes // no sense, because get_bit only returns // 0 or 1) return -1; } // Set the nth bit of the value of x to v. // Assume 0 <= n <= 31, and v is 0 or 1 void set_bit(unsigned * x, unsigned n, unsigned v) { // YOUR CODE HERE } // Flip the nth bit of the value of x. // Assume 0 <= n <= 31 void flip_bit(unsigned * x, unsigned n) { // YOUR CODE HERE } /* * YOU CAN IGNORE THE REST OF THIS FILE */ void test_get_bit(unsigned x, unsigned n, unsigned expected) { unsigned a = get_bit(x, n); if(a!=expected) { printf("get_bit(0x%08x,%u): 0x%08x, expected 0x%08x ",x,n,a,expected); } else { printf("get_bit(0x%08x,%u): 0x%08x, correct ",x,n,a); } } void test_set_bit(unsigned x, unsigned n, unsigned v, unsigned expected) { unsigned o = x; set_bit(&x, n, v); if(x!=expected) { printf("set_bit(0x%08x,%u,%u): 0x%08x, expected 0x%08x ",o,n,v,x,expected); } else { printf("set_bit(0x%08x,%u,%u): 0x%08x, correct ",o,n,v,x); } } void test_flip_bit(unsigned x, unsigned n, unsigned expected) { unsigned o = x; flip_bit(&x, n); if(x!=expected) { printf("flip_bit(0x%08x,%u): 0x%08x, expected 0x%08x ",o,n,x,expected); } else { printf("flip_bit(0x%08x,%u): 0x%08x, correct ",o,n,x); } } int main(int argc, const char * argv[]) { printf(" Testing get_bit() "); test_get_bit(0b1001110,0,0); test_get_bit(0b1001110,1,1); test_get_bit(0b1001110,5,0); test_get_bit(0b11011,3,1); test_get_bit(0b11011,2,0); test_get_bit(0b11011,9,0); printf(" Testing set_bit() "); test_set_bit(0b1001110,2,0,0b1001010); test_set_bit(0b1101101,0,0,0b1101100); test_set_bit(0b1001110,2,1,0b1001110); test_set_bit(0b1101101,0,1,0b1101101); test_set_bit(0b1001110,9,0,0b1001110); test_set_bit(0b1101101,4,0,0b1101101); test_set_bit(0b1001110,9,1,0b1001001110); test_set_bit(0b1101101,7,1,0b11101101); printf(" Testing flip_bit() "); test_flip_bit(0b1001110,0,0b1001111); test_flip_bit(0b1001110,1,0b1001100); test_flip_bit(0b1001110,2,0b1001010); test_flip_bit(0b1001110,5,0b1101110); test_flip_bit(0b1001110,9,0b1001001110); printf(" "); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!