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
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
- Class:
- PacketRepack
- Method:
- output
- Parameters:
- int[], int, int, int
- Returns:
- int[]
- Method signature:
- int[] output(int[] input, int b, int num_fields, int field_size)
- (be sure your method is public)
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
{ 22, 37, 3 }
6
4
4
Returns: {19, 22, 6 }
This is the example from the problem statement.
{ 1, 0 }
31
10
1
Returns: {512, 0 }
This 62-bit input packet has ten 1-bit fields, and 52 leading zeros. The first field (bit 0) is 1, and the other nine are 0. In the output, the tenth field (bit 9) should be set to 1, and all other bits should be 0.
{ 1, 0, 0, 0 }
10
31
1
Returns: {0, 0, 0, 1 }
{ 15834, 2483, 19423 }
16
8
6
Returns: {53074, 60455, 27516 }
{ 0 }
7
0
3
Returns: {0 }
Something for the challenge phase.
{ 0, 1, 2, 3, 4, 5, 6, 7, 0, 1 }
3
3
10
Returns: {7, 1, 2, 2, 4, 5, 2, 4, 0, 5 }
{ 2, 0 }
31
2
1
Returns: {1, 0 }
{ 475849283, 1847385748, 948372384, 293847528, 609182391, 839404358, 483923045, 1099348504, 839483390, 92394854 }
31
28
11
Returns: {429740384, 545253415, 1738662319, 73752641, 1952584370, 926779972, 124809603, 1544456309, 262479041, 151899655 }
{ 475849283, 1847385748, 948372384, 293847528, 609182391, 839404358, 483923045, 1099348504, 839483390, 92394854 }
31
11
28
Returns: {1925245619, 1751388927, 24775733, 34702235, 609163609, 1850084166, 256084362, 1618222012, 242174481, 414827654 }
{ 475849283, 1847385748, 948372384, 293847528, 609182391, 839404358, 483923045, 1099348504, 839483390, 92394854 }
31
44
7
Returns: {376783254, 1778219043, 104471065, 1482710407, 103646338, 584463332, 555676038, 2046871394, 1134876790, 283276228 }
{ 75849283, 47385748, 48372384, 93847528, 79182391, 39404358, 83923045, 99348504, 13483390, 2394854 }
30
15
20
Returns: {12585250, 922090214, 83983100, 37852184, 78681680, 552132422, 48326143, 141198312, 75543747, 360254612 }
{ 75849283, 47385748, 48372384, 93847528, 79182391, 39404358, 83923045, 99348504, 13483390, 2394854 }
30
20
15
Returns: {91422793, 515834267, 940313559, 137529857, 564331698, 488343920, 1072958255, 223348164, 105514406, 790726922 }
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 }
1
1
10
Returns: {0, 0, 0, 0, 0, 0, 1, 0, 0, 1 }
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 }
1
10
1
Returns: {1, 0, 0, 1, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 }
1
3
3
Returns: {0, 0, 1, 0, 0, 1, 0, 0, 0, 0 }
{ 0, 1, 0, 0, 0, 1, 0, 0, 1, 0 }
29
16
18
Returns: {0, 0, 4096, 0, 2048, 0, 0, 0, 0, 4 }
{ 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 }
31
310
1
Returns: {2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 }
{ 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483646 }
31
310
1
Returns: {1073741823, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 }
{ 2147483646, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647 }
31
310
1
Returns: {2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 2147483647, 1073741823 }
{1234, 4567, 7712, 7712, 99, 8844, 8889, 7723, 7723, 9911 }
31
310
1
Returns: {1991376896, 1782317056, 1782317056, 1319239680, 413270016, 1660944384, 37486592, 37486592, 1975779328, 630194176 }
{15834, 2483, 19423 }
16
8
6
Returns: {53074, 60455, 27516 }
{1, 0, 1, 0, 0, 1, 0, 1 }
1
4
2
Returns: {0, 1, 0, 1, 1, 0, 1, 0 }
{2147483647, 0 }
31
4
8
Returns: {2147483519, 1 }
{6 }
4
1
3
Returns: {6 }
{15834, 2483, 19423 }
17
7
7
Returns: {30667, 49592, 23286 }
{1834, 24839, 19423 }
16
8
6
Returns: {57170, 391, 43468 }
{0, 0 }
10
3
2
Returns: {0, 0 }
{1060627369, 597209938, 220, 0, 0, 0, 0, 0 }
30
4
17
Returns: {376158492, 891681212, 241, 0, 0, 0, 0, 0 }
{2602, 15843, 148 }
14
7
6
Returns: {14656, 12771, 10912 }
{1 }
1
1
1
Returns: {1 }
{1, 0 }
31
10
1
Returns: {512, 0 }
{22, 37, 3 }
6
4
4
Returns: {19, 22, 6 }
{10, 10, 10, 10, 10, 10 }
31
6
31
Returns: {10, 10, 10, 10, 10, 10 }
{15834, 24836, 0 }
22
8
6
Returns: {397376, 2999040, 6 }