1. Engineering
  2. Computer Science
  3. each function should perform a series of bitwise operations usually...

Question: each function should perform a series of bitwise operations usually...

Question details
Each function should perform a series of bitwise operations (usually just one) on an integer, and return the resulting integer.

It needs to be done using ONLY bitwise operations, except for the following functions where an "if" statement is okay:

  • ifBit7is0
  • ifBit3is1
  • signedBits0through5
  • signedBits6through9

Here is a table showing examples of the output for each function:

Function Input Output
multiplyBy8 7 56
setBit6to1 0x01 0x41
setBit3to0 0xffff 0xfff7
flipBit5 0x01 0x21
ifBit7is0 0x77 nonzero
ifBit3is1 0x77 0
unsignedBits0through5 0x335 0x35
unsignedBits6through9 0x3b4 0xe
signedBits0through5 0x3fa 0xfffffffa
signedBits6through9 0xcf7 0x03
signedBits0through5 0x38f 0xf
signedBits6through9 0x38f 0xfffffffe
setBits4through9 (0xffffffff, 0x23) 0xfffffe3f

The function that needs the most explanation is setBits4through9. Think of this as replacing bits 4-9 (inclusive) with the second input's lowest bits. Leave the rest of the input's bits unchanged.

Any extracted bits should be returned in the lowest bits of the return value. For example, in unsignedBits6through9, if bits 0110 were extracted from the value, then 0110 with 28 leading zeros should be returned (we are working with 32-bit integers).

Here is the .c file that needs to be edited...

#include "stdio.h"
#include "stdlib.h"

int multiplyBy8(int v)
{
        // TODO: multiply the number by 8
        return v;
}

int setBit6to1(int v)
{
        // TODO: set bit 6 to 1
        return v;
}

int setBit3to0(int v)
{
        // TODO: set bit 3 to 0
        return v;
}
        
int flipBit5(int v)
{
        // TODO: flip bit 5 (if it is 0, make it 1.  If 0, make 1)
        return v;
}
        
int ifBit7is0(int v)
{
        // TODO: check to see if bit 7 is a 0 - return 1 if true, 0 if false
        return v;
}

int ifBit3is1(int v)
{
        // check to see if bit 3 is a 1 - return 1 if true, 0 if false
        return v;
}

int unsignedBits0through5(int v)
{
        // return the unsigned value in bits 0 through 5
        return v;
}

int unsignedBits6through9(int v)
{
        // return the unsigned value in bits 6 through 9
        return v;
}

int signedBits0through5(int v)
{
        // return the signed value in bits 0 through 5
        return v;
}

int signedBits6through9(int v)
{
        // return the signed value in bits 6 through 9
        return v;
}

int setBits4through9(int v, int setValue)
{
        // set bits 4 through 9 in v to become setValue
        return v;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution