forked from rootusercop/IMP-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find whether a bit is set in Character or Integer
99 lines (87 loc) · 3.63 KB
/
Find whether a bit is set in Character or Integer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Question: Program to find whether a bit is SET or UNSET(Not SET) in character and integer values.
Logic:
Understand the code mentioned in all the below mentioned links:
I. For Reference you can understand the program mentioned in the link: http://www.geeksforgeeks.org/power-set/
II. How to check whether a bit is set in JAVA OR C OR ANY Programming Language:(See both the below mentioned links)
1.http://stackoverflow.com/questions/1092411/java-checking-if-a-bit-is-0-or-1-in-a-long
2.http://stackoverflow.com/questions/13420241/check-if-one-single-bit-only-is-set-within-a-64-bits-integer
Solution:
package CheckForSetBitsInIntegerAndCharacter;
import java.util.Scanner;
public class UsingLeftShift {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
try{
while(true){
System.out.println("Program to check which of the first 8 bits (counting from right to left) "+
"are SET or UNSET");
System.out.println("1. Character \n2. Integer \n3. END");
System.out.println("Enter choice");
int choice = in.nextInt();
switch(choice){
case 1:
System.out.println("Checking set bits in Characters:");
System.out.println("Enter the string");
String s = in.next();
char[] charArray = s.toCharArray();
printSetBits(charArray);
break;
case 2:
System.out.println("Checking set bits in Integers:");
System.out.println("Enter the size of the integer array");
int n = in.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements of the integer array");
for(int i=0;i<n;i++)
a[i]=in.nextInt();
printSetBits(a);
break;
case 3:
System.out.println("Program Ended");
System.exit(0);
default:
System.out.println("Wrong choice");
break;
}
}
}
finally{
in.close();
}
}
private static void printSetBits(char[] c) {
for(int i=0;i<c.length;i++){
System.out.println("Character: "+c[i]);
System.out.println("Ascii Value "+(int)c[i]);
System.out.println("Consider counting of bits from right to left");
System.out.println("Binary Representation "+Integer.toBinaryString((int)c[i]));
for(int j=0;j<8;j++){ // j is the bitCounter which starts from 0000 and goes till 0111
// This is to check which of the first 8 bits (counting from right to left) are SET or UNSET
System.out.print("For the "+j+"th bit: ");
if((c[i]&(1<<j))!=0) // 1 is left shifted by j positions, i.e. 1<<0 = 1
System.out.print("SET"); // i.e. 1<<1 = 10
else // i.e. 1<<2 = 100
System.out.print("UNSET"); // i.e. 1<<3 = 1000
System.out.println(); // i.e. 1<<4 = 10000
} // i.e. 1<<5 = 100000
System.out.println(); // i.e. 1<<6 = 1000000
} // i.e. 1<<7 = 10000000
}
private static void printSetBits(int[] a) {
for(int i=0;i<a.length;i++){
System.out.println("Integer: "+a[i]);
System.out.println("Consider counting of bits from right to left");
System.out.println("Binary Representation "+Integer.toBinaryString(a[i]));
for(int j=0;j<8;j++){ // j is the bitCounter which starts from 0000 and goes till 0111
//This is to check which of the first 8 bits (counting from right to left) are SET or UNSET
System.out.print("For the "+j+"th bit: ");
if((a[i]&(1<<j))!=0) // 1 is left shifted by j positions, i.e. 1<<0 = 1
System.out.print("SET"); // i.e. 1<<1 = 10
else // i.e. 1<<2 = 100
System.out.print("UNSET"); // i.e. 1<<3 = 1000
System.out.println(); // i.e. 1<<4 = 10000
} // i.e. 1<<5 = 100000
System.out.println(); // i.e. 1<<6 = 1000000
} // i.e. 1<<7 = 10000000
}
}