Suppose the cable company represents the channels your TV has access to with a 6
ID: 3873626 • Letter: S
Question
Suppose 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.
In CableCompany.java, write code for the following tasks. You may not call any “magic” library functions or use loops. Rather you should just use basic operations like creating and assigning variables, if statements, and the bitwise operations (~, &, <<, >>>, >>, |, ^).
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) { … }
Explanation / Answer
a).
public static boolean isEnabled(long A, int channel)
{
long ans=(A>>channel)&1;
/* right shifting A by channel number of times makes the bit at the channel position in the binary representaation of A as the least significant bit and then logical 'AND' with '1' gives the desired result.
Eg. Suppose A=5 and channnel=1; binary representation of A is 0000...101 and 1 is 000.....1. Now,right shift A by
channel(here,channel=1) number of times. Then, it would become 000....10.
Now, A=000....101
A>>channel= 000....010
logical AND = 000....000 =false. -> The bit at channel 1 in 000....101 is not set.
*/
if(ans==1)
return true;
else
return false;
}
b)
public static long enableChannel(long A, int channel)
{
A=A|(1<<channel);
/* left shifting '1' by channel number of times and then logical 'OR' with the given number gives the desired result Eg. Suppose A=5 and channnel=1; binary representation of A is 0000...101 and 1 is 000.....1. Now,left shift 1 by
channel(here,channel=1) number of times. Then, it would become 000....10.
Now, A=000....101
1<<channel= 000....010
logical OR = 000....111 =7.
*/
return A;
}
SInce, you have only asked for the methods, I am not providing the main method and just only the methods.
My whole program is
import java.util.Scanner;
class Scanner1
{
public static boolean isEnabled(long A, int channel)
{
long ans=(A>>channel)&1;
/* right shifting A by channel number of times makes the bit at the channel position in the binary representaation of A as the least significant bit and then logical 'AND' with '1' gives the desired result.
Eg. Suppose A=5 and channnel=1; binary representation of A is 0000...101 and 1 is 000.....1. Now,right shift A by
channel(here,channel=1) number of times. Then, it would become 000....10.
Now, A=000....101
A>>channel= 000....010
logical AND = 000....000 =false. -> The bit at channel 1 in 000....101 is not set.
*/
if(ans==1)
return true;
else
return false;
}
public static long enableChannel(long A, int channel)
{
A=A|(1<<channel);
/* left shifting '1' by channel number of times and then logical 'OR' with the given number gives the desired result Eg. Suppose A=5 and channnel=1; binary representation of A is 0000...101 and 1 is 000.....1. Now,left shift 1 by
channel(here,channel=1) number of times. Then, it would become 000....10.
Now, A=000....101
1<<channel= 000....010
logical OR = 000....111 =7.
*/
return A;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
while(true)
{
long A=sc.nextInt();
int channel=sc.nextInt();
boolean ans=isEnabled(A,channel);
System.out.println(ans);
long newans=enableChannel(A,channel);
System.out.println(newans);
}
}
}
/*IN the main method,I am executing an infinite loop which takes the long 'A' and the 'channel' and first calls the isEnabled method and then the enableChannel method to set the bit and then prints the result of the two. Remember, main() method for this problem can be implemented in various different ways as you have not mentioned the specifications for the main method. If you use my main method , then the only way to exit the program is by pressing Ctrl-C.*/
Commands(LInux)
To compile: javac <filename>.java
To run: java <filename>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.