4
1
Introduction
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.
Challenge
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 0
.
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
1
. - The fourth argument (manipulator only) will be the manipulation mode:
- When passed a
0
or 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
1
. - When passed a negative value, the behavior will be to set all of the specified bits to
0
.
- 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
0
. - 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.
Examples
Here's a few examples of input and output that your code should be able to handle. I've golfed both functions in JavaScript that I can release if people want to test their results, but otherwise I'll probably wait a few days before I post it up.
Manipulator
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)
Reader
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)
What if a language doesn't support optional function arguments? – Zgarb – 2016-03-01T16:46:07.220
@Zgarg Then you can pass in the default values
1
and0
, respectively. – Mwr247 – 2016-03-01T16:47:56.053Ok, thanks. Also, can we have shared code between the two functions, and can one of them use the other in its definition? – Zgarb – 2016-03-01T16:48:52.770
@Zgarb I'll allow that, so long as they are still called as specified. Your function can reference another function, and your program can reference another program, but all of the code used must be summed up in the end. – Mwr247 – 2016-03-01T16:54:11.953
1You should add some test cases that use 32-bit integers, since that it the requirement. – mbomb007 – 2016-03-01T21:19:38.573
@mbomb007 Good point. Added some simple cases to just ensure they work at that level. Thanks! – Mwr247 – 2016-03-04T17:48:44.227