There are often times that we have challenges which require communication of some sort (such as in team KOTH), where density of information may be more important than density of code. There are a number of compression techniques that can allow us to convey more information in less space, but for simple data flags, sometimes flipping bits themselves can be a great way to achieve desired data density.
Write a program or function in the language of your choice that can take an integer to be manipulated (must handle at least 32 bit), a start bit position (integer), a number of bits to manipulate (integer), and a manipulation mode (integer), and returns the original integer manipulated in the way specified below. In addition, write a second program or function that serves as a bit reader, taking in an integer, a start bit position (integer), and a number of bits to read (integer), and returns an integer consisting of just that group of bits shifted down, and every other bit as
Some further details and rules are as follows:
- The first argument will be an integer, signed or unsigned.
- The second argument will be the bit position to start from (counting from least significant bit, zero-indexed).
- The third argument will be the number of bits to be read/manipulated, moving up in bit significance from the start bit. If not given as an argument, the value should default to
- The fourth argument (manipulator only) will be the manipulation mode:
- When passed a
0or null value, the behavior will be to flip (xor) all of the specified bits (default if no value given).
- When passed a positive value, the behavior will be to set all of the specified bits to
- When passed a negative value, the behavior will be to set all of the specified bits to
- When passed a
- The return value of the manipulator function should be the initial integer with the above bit manipulations applied.
- The return value of the reader function should be the start bit shifted to the least significant bit, and every bit beyond the length of bits set to
- Assume that only valid input will be used. Don't worry about error handling.
The winner will be determined by the shortest sum of the length of both programs/functions. They will be selected in at least 7 days, or if/when there have been enough submissions. In the event of a tie, the answer that came earlier wins.
F(0, 2) => 4 (0 => 100) F(4, 2) => 0 (100 => 0) F(0, 2, 4) => 60 (0 => 111100) F(170, 2, 4, 0) => 150 (10101010 => 10010110) F(170, 2, 4, 1) => 190 (10101010 => 10111110) F(170, 2, 4, -1) => 130 (10101010 => 10000010) F(0, 31) => (+/-)2147483648 (0 => 10000000000000000000000000000000)
F(0, 2) => 0 (0 => 0) F(4, 2) => 1 (100 => 1) F(60, 2, 4) => 15 (111100 => 1111) F(170, 2, 4) => 10 (10101010 => 1010) F((+/-)2147483648, 0, 32) => (+/-)2147483648 (10000000000000000000000000000000 => 10000000000000000000000000000000)