I have a hardware that has a 32-bit input register. The register has the following characteristics:
- The 32-bit register consists of eight 4-bit fields.
- Each 4-bit field holds a value in signed-magnitude; it can hold an integer between -7 and +7 inclusive, including -0 and +0 (signed zeroes).
For example, the hexadecimal value 0xABCD1234
represents the field values [-2, -3, -4, -5, +1, +2, +3, +4]
. In fact, one hex digit represents one 4-bit value with the following mapping:
Hex | Input value
---------------------
0 ~ 7 | +0 ~ +7
8 ~ F | -0 ~ -7
In order to operate this hardware, I figured that the most natural inputs would be 8 consecutive increasing values, treating -0 and +0 as distinct. So the input values will be one of the following:
field values for 8 fields => 32-bit register value
[-7, -6, -5, -4, -3, -2, -1, -0] => 0xFEDCBA98 (offset -4)
[-6, -5, -4, -3, -2, -1, -0, +0] => 0xEDCBA980 (offset -3)
...
[-3, -2, -1, -0, +0, +1, +2, +3] => 0xBA980123 (balanced, offset 0)
...
[+0, +1, +2, +3, +4, +5, +6, +7] => 0x01234567 (offset +4)
I define the input 0xBA980123
as balanced, and the other inputs have an offset from the balanced input. The balanced input itself has offset 0.
Task
Given the offset, output the desired value for the input register for my hardware as described above.
Input and output
The input (offset) is an integer between -4 and 4 inclusive. You can take it as a string, optionally with explicit sign (e.g. +4
).
You can output the result value as an integer or a string. If you choose to output as a string (either returning or printing), you can use base 2, 8, 10 or 16. In case of hexadecimal, mixing upper- and lowercase letters are allowed. Base prefixes (e.g. 0b
or 0x
), leading zeros, and leading/trailing whitespaces are also allowed.
You may use signed 32-bit values for output instead (imagine the values being used in a C program); in that case, the output values will be subtracted by 2^32
, except for the input +4
.
Scoring
The standard code-golf rules apply. The shortest code in bytes wins.
All test cases
There are only nine possible inputs, so here is the list of all input/output values your program/function needs to support.
input => output hex | decimal | signed 32bit
-4 => 0xFEDCBA98 | 4275878552 | -19088744
-3 => 0xEDCBA980 | 3989547392 | -305419904
-2 => 0xDCBA9801 | 3703216129 | -591751167
-1 => 0xCBA98012 | 3416883218 | -878084078
0 => 0xBA980123 | 3130523939 | -1164443357
1 => 0xA9801234 | 2843742772 | -1451224524
2 => 0x98012345 | 2550211397 | -1744755899
3 => 0x80123456 | 2148676694 | -2146290602
4 => 0x01234567 | 19088743 | 19088743
0
->[11,10,9,8,0,1,2,3]
as a raw hexadecimal notation) \$\endgroup\$