Analyze this code and run it, if there are any issues note them and fix them, if not give the output and Big O notation runtime:
public class PrintBits {
public static void printBits(int a) {
try {
String str = Integer.toBinaryString((Integer) a);
for(int i = 0; i < str.length(); i++){
System.out.print (str.charAt(i));
}
}
catch (ClassCastException e) {
throw new RuntimeException ("Argument is not an Integer");
}
}
public static void main (String[] args){
printBits (-17);
System.out.println();
printBits (17);
System.out.println( );
}
}
/*If you have any query do comment in the comment section else like the solution*/
Output:
-17: 11111111111111111111111111101111
17: 10001
Concept of time complexity comes for a variable which tends to infinity, but the maximum value that can be passed to toBinaryString() method is 231-1, so it is illogical to ask about time complexity of above algorithm, also the string returned by toBinaryString() method will always be less than or equal to 32, hence we can not take time complexity into consideration here.
Get Answers For Free
Most questions answered within 1 hours.