Skip to content

Commit 36c8cf5

Browse files
committed
Bitwise Two
1 parent 5ce3291 commit 36c8cf5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/easy/BitwiseTwo.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package easy;
2+
3+
/**
4+
* Have the function BitwiseTwo(strArr) take the array of strings stored in strArr,
5+
* which will only contain two strings of equal length that represent binary numbers,
6+
* and return a final binary string that performed the bitwise AND operation on both strings.
7+
* ---
8+
* A bitwise AND operation places a 1 in the new string where
9+
* there is a 1 in both locations in the binary strings,
10+
* otherwise it places a 0 in that spot. For example:
11+
* if strArr is ["10111", "01101"] then your program should return the string "00101"
12+
*/
13+
public class BitwiseTwo {
14+
15+
/**
16+
* Bitwise Two function.
17+
*
18+
* @param strArr an array of two binary strings
19+
* @return a string that performed the bitwise AND operation on both strings
20+
*/
21+
private static String bitwiseTwo(String[] strArr) {
22+
23+
String s1 = strArr[0];
24+
String s2 = strArr[1];
25+
StringBuilder out = new StringBuilder();
26+
27+
for (int i = 0; i < s1.length(); i++) {
28+
int lgAnd = Character.getNumericValue(s1.charAt(i)) & Character.getNumericValue(s2.charAt(i));
29+
out.append(lgAnd);
30+
}
31+
32+
return out.toString();
33+
}
34+
35+
/**
36+
* Entry point.
37+
*
38+
* @param args command line arguments
39+
*/
40+
public static void main(String[] args) {
41+
var result1 = bitwiseTwo(new String[]{"10111", "01101"});
42+
System.out.println(result1);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)