Problem Statement | |||||||||||||
You are writing a function to make two pieces of network equipment from different vendors talk to each other. They send and receive data packets with the same data fields, but arranged in opposite orders. Given a data packet from one piece of equipment, reverse the order of the data fields so the second piece of equipment can read it. The input data will be packed into N b-bit words, for a total of N*b bits. These words will be given to you as a int[] input. The bit 0 of input[0] is bit 0 of the data packet, and bit b-1 of input[N-1] is bit N*b-1 of the data packet. There will be num_fields fields, each field_size bits long. The first field is packed into bits 0 through field_size-1, the second field is packed into bits field_size through field_size*2-1, etc. For example, given an input packet of { 22, 37, 3 }, with 6-bit words, and four 4-bit fields, the fields would be extracted as shown below: input[2] input[1] input[0] | | | ----------- ----------- ----------- 0 0 0 0 1 1 1 0 0 1 0 1 0 1 0 1 1 0 ------- ------- ------- ------- | | | | D C B A Where A is the first field, B is the second field, C is the third field, and D is the forth field. These fields have the values 6, 5, 9, and 3, respectively. Reversing the order, the fields would be repacked like this: output[2] output[1] output[0] | | | ----------- ----------- ----------- 0 0 0 1 1 0 0 1 0 1 1 0 0 1 0 0 1 1 ------- ------- ------- ------- | | | | A B C D and the correct output is { 19, 22, 6 }. As shown in the example above, the upper bits of the packet may be unused. These bits will be zero in the input, and must be set to zero in the output as well. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | input will contain between 1 and 10 elements, inclusive. | ||||||||||||
- | Each element of input will be between 0 and 2^b-1, inclusive. | ||||||||||||
- | b will be between 1 and 31, inclusive. | ||||||||||||
- | field_size will be between 1 and 31, inclusive. | ||||||||||||
- | The size of input multipled by b will be greater than or equal to num_fields*field_size. | ||||||||||||
- | Unused bits in the packet will be zeroes. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|