6
3
Given an array of 32-bit integers which represent a bit array, shift the values by a specified amount in either direction. Positive shifts go to the left and negative values go to the right. This is a bit array so bits should wrap across values in the array.
For example, given the following data:
a[2] = 11110000 11110000 11110000 11110000
a[1] = 11110000 11110000 11110000 11110000
a[0] = 11110000 11110000 11110000 11110000 <-- 0th bit
Shifting the original by 2 should result in:
a[3] = 00000000 00000000 00000000 00000011
a[2] = 11000011 11000011 11000011 11000011
a[1] = 11000011 11000011 11000011 11000011
a[0] = 11000011 11000011 11000011 11000000 <-- 0th bit
Shifting the original by -3 should result in:
a[2] = 00011110 00011110 00011110 00011110
a[1] = 00011110 00011110 00011110 00011110
a[0] = 00011110 00011110 00011110 00011110 <-- 0th bit
There's no restriction on shift amount (i.e., can be greater than 32) so be mindful of where your bits are going!
Here's the method signature:
int[] bitArrayShift(int[] bits, int shift)
Implementations should:
- Return an array whose contents represented the original bit array's contents shifted by the supplied number.
- Return an array whose size is the minimum amount required to hold the values after the shift. You should not wrap back around to the 0th bit but rather expand to hold it.
- Not mutate the input array. It should be treated as being passed by reference (therefore having mutable contents). Basically, don't put any new values at any position in the input array.
The following assertion should always return true for any value of 'bit' and 'shift':
// Given 'originalBits' as incoming bit array.
// Given 'bit' as a number between [0, originalBits.length * 32).
// Given 'shift' as any integer value.
// Get the value in the array at the index of 'bit'
bool original = ((originalBits[bit / 32] & (1 << (bit % 32)) != 0
// Shift the original bits by the specified 'shift'
int[] shiftedBits = bitArrayShift(originalBits, shift)
// Offset the original 'bit' by the specified 'shift'
int shiftedBit = bit + shift
// Get the value in the shifted array at the index of 'shiftedBit'
bool shifted = ((shiftedBits[shiftedBit / 32] & (1 << (shiftedBit % 32)) != 0
// They should be equal.
assert original == shifted
Smallest character count wins. Count method body contents ONLY sans leading and trailing whitespace. That is, the method declaration up to and including the block start (if any) and ending exclusively at the block end (if any).
E.g., for Java:
public static int[] shiftBitArray(int[] bits, int shift) {
<< COUNT THIS ONLY >>>
}
1"therefore having mutable contents". What exactly does this mean? Did you mean immutable? – Justin – 2014-01-25T05:14:31.847
Should leading zeroes be stripped (ie is [1][0] an allowed result instead of just [1]? – Joachim Isaksson – 2014-01-25T13:45:23.207
Your example of shifting the original array by 2 does not wrap across values in the array. It augments the array thereby changing its dimensions. Isn't this incorrect? – DavidC – 2014-01-25T17:44:09.020
The question does not completely define how wrapping occurs in the negative direction. What is the result of shifting by -5? Assuming the intention is to prepend a new index at the beginning, similarly to the second example which appended a new index. – James Wald – 2014-01-25T21:09:21.640