Question

In: Computer Science

Suppose the cable company represents the channels your TV has access to with a 32-bit integer....

Suppose the cable company represents the channels your TV has access to with a 32-bit integer. Each channel from 0 to 31is represented by one bit, where 1 means the channel is enabled and 0 means the channel is disabled. Assume channel 0 is the least significant bit. When you get your cable box, the technician sets the 32-bit code.

// for example, this code enables channels 0, 1, and 2 only intcode= 7;

In cableCompany.c, write code for the following tasks. You may not call any “magic” library functions and you may not use any loops. Rather you should just use basic operations like creating and assigning variables, equality (==), ifstatements, and the bitwise operations (~, &, <<, >>, |, ^)

.a.A function that returns whether a particular channel (integer 0-31) is enabled. boolisEnabled(intA, charchannel) { ... }

b.A function that returns a new integer, with the given channel enabled.intenableChannel(intA, charchannel) { ... }

Solutions

Expert Solution

Following is the code with both methods, plus a driver function to understand things :

#include <stdio.h>
#include <stdbool.h>



bool isEnabled(int A, char channel)
{
    /*
     * To check if a bit/channel is set in A, we use
     * bitwise-ANDing with a suitable mask, and then check
     * whether the value is equal to 0.
     *
     * We proceed step by step.
     */

    /*
     * 1. Create a mask, with
     *  
     *      * 1 at position, for the channel to be checked.
     *      * 0 at all other positions.
     */
    unsigned int mask = 1U << channel;

    /*
     * 2. Do a bitwise-AND with A.
     *    If the value is equal to 0, it means bit/channel is not set.
     */
    if( (A & mask) == 0)
    {
        return false;
    }

    /*
     * The value is not 0, thus the bit/channel is set.
     */
    return true;
}


int enableChannel(int A, char channel)
{
    /*
     * To set a bit/channel is set in A, we use
     * bitwise-ORing with a suitable mask.
     *
     * We proceed step by step.
     */

    /*
     * 1. Create a mask, with
     *  
     *      * 1 at position, for the channel to be enabled.
     *      * 0 at all other positions.
     */
    unsigned int mask = 1U << channel;

    /*
     * 2. Do a bitwise-OR with A, and return the result.
     */
    return (A | mask);
}


/*
 * Driver to test ..
 */
int main()
{
    int A = 0;

    printf("\n\nIs channel 9 set : %d\n", isEnabled(A, 9));
    printf("Is channel 22 set : %d\n", isEnabled(A, 22));
    printf("Is channel 31 set : %d\n", isEnabled(A, 31));

    printf("\n\nSetting channel 31.\n\n");
    A = enableChannel(A, 31);

    printf("Is channel 9 set : %d\n", isEnabled(A, 9));
    printf("Is channel 22 set : %d\n", isEnabled(A, 22));
    printf("Is channel 31 set : %d\n", isEnabled(A, 31));

    printf("\n\nSetting channel 22.\n\n");
    A = enableChannel(A, 22);

    printf("Is channel 9 set : %d\n", isEnabled(A, 9));
    printf("Is channel 22 set : %d\n", isEnabled(A, 22));
    printf("Is channel 31 set : %d\n", isEnabled(A, 31));

    printf("\n\n");
    return 0;
}

Following is the output :



Is channel 9 set : 0
Is channel 22 set : 0
Is channel 31 set : 0


Setting channel 31.

Is channel 9 set : 0
Is channel 22 set : 0
Is channel 31 set : 1


Setting channel 22.

Is channel 9 set : 0
Is channel 22 set : 1
Is channel 31 set : 1



Related Solutions

Encode 32-bit INTEGER 12 in TLV format?
Encode 32-bit INTEGER 12 in TLV format?
A telecommunications company provided its cable TV subscribers with free access to a new sports channel...
A telecommunications company provided its cable TV subscribers with free access to a new sports channel for a period of 1 month. It then chose a sample of 398 television viewers and asked them whether they would be willing to pay an extra $10 per month to continue to access the channel. A total of 27 of the 398 replied that they would be willing to pay. The marketing director of the company claims that the percentage of all of...
The following data set represents Heather’s average monthly charges ( in $) for cable TV for...
The following data set represents Heather’s average monthly charges ( in $) for cable TV for the past 24 months 105 125 110 98 102 115 110 123 118 101 95 128 110 105 122 107 118 107 117 125 116 110 101 107 A) Construct a Stemplot. B) Construct a frequency distribution , class width of 10, and a lower limit of 95 for class 1. C) Construct a histogram. Comment on the shape of the distribution based on...
Suppose Natasha currently makes $50,000 per year working as a manager at a cable TV company....
Suppose Natasha currently makes $50,000 per year working as a manager at a cable TV company. She then develops two possible entrepreneurial business opportunities. In one, she will quit her job to start an organic soap company. In the other, she will try to develop an Internet-based competitor to the local cable company. For the soap-making opportunity, she anticipates annual revenue of $465,000 and costs for the necessary land, labor, and capital of $435,000 per year. For the Internet opportunity,...
You are a manager of a company that sells cable television channels. You are thinking of...
You are a manager of a company that sells cable television channels. You are thinking of bundling a nature channel and a travel channel. Suppose that the marginal cost of each channel is zero. You have equal numbers of two types of customers. The table lists the maximum monthly price each type is willing to pay for each product. Customer Maximum price willing to pay for a nature channel Maximum price willing to pay for a travel channel A $0.75...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and initialize it with the smallest possible negative decimal value. b. Declare an uninitialized array of 100 16-bit unsigned integers. c. Declare a string variable containing the word “DVC” repeated 20 times, and terminated with the null char. Question 2 For the following declarations, assuming that the address of I is 404000h What are the addresses of J, K, and L? What is the total...
1. Suppose Natasha currently makes $50,000 per year working as a manager at a cable TV...
1. Suppose Natasha currently makes $50,000 per year working as a manager at a cable TV company. She then develops two possible entrepreneurial business opportunities. In one, she will quit her job to start an organic soap company. In the other, she will try to develop an Internet-based competitor to the local cable company. For the soap-making opportunity, she anticipates annual revenue of $465,000 and costs for the necessary land, labor, and capital of $395,000 per year. For the Internet...
You manage a cable company that offers 2 channels - NBC and Fox.You face 2...
You manage a cable company that offers 2 channels - NBC and Fox. You face 2 types of customers (type A and type B) and there are 100 customers of each type. Their respective values for each channel are: Type A Type B NBC $10 $15 Fox $3 $7 If you bundle the two channels together, what price should you charge for the bundle? (Write answer without the dollar sign.)
ou manage a cable company that offers 2 channels - NBC and Fox.You face 2...
ou manage a cable company that offers 2 channels - NBC and Fox. You face 2 types of customers (type A and type B) and there are 100 customers of each type. Their respective values for each channel are:Type AType BNBC$10$15Fox$3$7If the marginal cost of selling each channel is $1 per channel, what is the most profitable strategy?sell the channels separatelybundle the channelsindifferent between selling separately and bundling the channels
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0x00156A33
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT