CMPS 144 Fall 2023
Quiz #1
|
|
Name __________________________
|
| 1011101001 |
⊕ | 1100110011 |
| 0111011010 |
|
| 000101110 |
⊕ | 100110011 |
| 100011101 |
|
For the purposes of this quiz, we define a bit vector to be a
sequence of 0's and 1's.
Among the ways to represent a bit vector is via an array of type
int[] or an object of type ArrayList<Integer>,
each of whose elements has value either 0 or 1.
A common operation applied to a pair of bit vectors is
bitwise exclusive or (XOR), often denoted by ⊕.
To the right are two examples.
The rule for producing the resultant bit vector is that (for all k)
its k-th bit is 1 if and only if the sum of the k-th bits of the
two operands is 1.
Supply the missing body of the Java method below, which has two alternatives
as to its heading. Make your choice as to which heading to "comment out".
For the sake of simplicity, you may assume, with only a small penalty,
that the two bit vectors are of the same length. Full credit can be
achieved by allowing for them to be of different lengths. In that
case, assume that all the "missing" trailing bits in the shorter vector
are 0's. You may want to introduce a separate method that, given a
bit vector and an integer k, returns the bit at position k of the
vector, or zero if that position does not exist.
/* Given as inputs two bit vectors, returns the bit vector resulting
** from applying the bitwise exclusive or operation.
*/
public static int[] bitwiseXOR(int[] A, int[] B) {
public static ArrayList<Integer> bitwiseXOR(ArrayList<Integer> A,
ArrayList<Integer> B) {
|