|
| 1 | +package easy; |
| 2 | + |
| 3 | +/** |
| 4 | + * Have the function BitwiseOne(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 OR operation |
| 7 | + * on both strings. |
| 8 | + * --- |
| 9 | + * A bitwise OR operation places a 0 in the new string |
| 10 | + * where there are zeroes in both binary strings, |
| 11 | + * otherwise it places a 1 in that spot. |
| 12 | + * --- |
| 13 | + * For example: if strArr is ["1001", "0100"] then your program |
| 14 | + * should return the string "1101" |
| 15 | + */ |
| 16 | +public class BitwiseOne { |
| 17 | + |
| 18 | + /** |
| 19 | + * Bitwise One function. |
| 20 | + * |
| 21 | + * @param strArr an array of two binary strings |
| 22 | + * @return a binary string that performed the bitwise OR operation on both strings |
| 23 | + */ |
| 24 | + private static String bitwiseOne(String[] strArr) { |
| 25 | + String s1 = strArr[0]; |
| 26 | + String s2 = strArr[1]; |
| 27 | + StringBuilder result = new StringBuilder(); |
| 28 | + for (int i = 0; i < s1.length(); i++) { |
| 29 | + int lgOr = Character.getNumericValue(s1.charAt(i)) | Character.getNumericValue(s2.charAt(i)); |
| 30 | + result.append(lgOr); |
| 31 | + } |
| 32 | + return result.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 = bitwiseOne(new String[]{"001110", "100000"}); |
| 42 | + System.out.println(result1); |
| 43 | + var result2 = bitwiseOne(new String[]{"110001", "111100"}); |
| 44 | + System.out.println(result2); |
| 45 | + } |
| 46 | + |
| 47 | +} |
0 commit comments