Suppose I the cable company represents the channels your TV

Suppose I the cable company represents the channels your TV has access to with a 64- bit integer. Each channel from 0 to 63 is 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 64-bit code. long A = enabledChannels(); Write code (pseudocode or Java is fine) for the following tasks. You may not call any “magic” library functions, rather you should just use basic operations like if, for, while, the bitwise operations (~, &, <<, >>, |, ^) and integer operations, like ==, <, and >.

a. A function that returns whether a particular channel (integer 0-63) is enabled. boolean isEnabled(long A, int channel) { … }

b. A function that returns a new integer, with the given channel enabled. long enableChannel(long A, int channel) { … }

Solution

//Pseudocode for the two functions is given below

// long int A is 64 bit

bool isEnabled(long int A, int channel)
{
vector<int> code;
int r;
//convert long A into 64 bit code
for(int i = 0; i<=63; i++)
{
r = A%2;
code.push_back(r);
A = A/2;
}
//check if the channel is active or not
if(code[channel] == 1)
{
return true;
}
else
{
return false;
}
}

long int enableChannel(long int A, int channel)
{
long int new_int = 0;
vector<int> code(64,0); //initialise a vector with all channels disabled
code[channel] = 1; //enable given channel
//convert code into integer   
for(int i = 0; i<=63; i++)
{
new_int = new_int + code[i]*pow(2,i);
}   
return new_int;
}

Suppose I the cable company represents the channels your TV has access to with a 64- bit integer. Each channel from 0 to 63 is represented by one bit, where 1 m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site