Biplex: an important useless operator
Pyth, 26 25 bytes
JsM.T_MjR2Qi_}RhM_BS-J0J2
Try it online: Demonstration or Test Suite
Explanation
JsM.T_MjR2Q
jR2Q convert each input number to binary (lists of 1s and 0s)
_M reverse each list
.T transpose (with top justify)
sM sum up each list (numbers of 1s at each position)
J store this list in J
i_}RhM_BS-J0J2
-J0 J without 0s
S sorted
_B create the list [sorted, reverse(sorted)]
hM take the first elments of each (bitsum-min and bitsum-max)
}R J check for each value in J, if it is part of ^
_ reverse this list of booleans
i 2 convert from binary to base 10 and print
Minkolang 0.10, 109 79 bytes
(n1$(d2%$r2:d)99*I-DmI2:[+1R]$I)rI$d$(s0(x0gd,)Ik3R2g1-X0I3-[2*2gd0c=$r1c=++]N.
Input and output are in decimal. Try it here.
Explanation
( $I) Loop until input is empty
n Read in number from input
1$( d) Start a while loop with only the top of stack
d2% Next least-significant bit (modulo by 2)
$r Swap top two values
2: Divide by 2
99*I-D Pad with 0s
(when the while loop exits, top of stack is 0)
m Merge (interleave) stack
I2:[ ] For each pair...
+1R Add and rotate stack to the right
<<This does the work of computing bitsums>>
r Reverse stack
I Push length of stack
$d Duplicate whole stack
<<This lets me sort the bitsums without affecting the original order>>
$( Start while loop with <top of stack> elements
s Sort
0( ) Push a 0 and start another while loop
x Dump top of stack
0g Get front of stack and put it on top
d, Duplicate and <not> for end-while condition check
Ik Push length of stack and break out of while loop
3R Rotate [min, max, length] to front
2g1- Get length and put it on top, then subtract 1
X Dump that many elements off the top of stack
<<Now I have min and max>>
0 Initialize decimal with 0
I3-[ ] For as many bits as there are...
2* Multiply by 2
2gd Get the next most significant bit and put it on top
0c= Copy min and check for equality
$r Swap top two elements of stack
1c= Copy max and check for equality
++ Add 1 if bitsum item is equal to min or max, 0 otherwise
N. Output as integer and stop
Old version:
$nI[(d2%i1+0a+i1+0A00ai`4&i00A2:d)x]00a1+[i1+0a]s0(x0gd,)rI2-[x]00a1+[i1+0ad0c=$r1c=+]0gx0gx0(xd,)0I1-[2*+]N.
Try it here!
Explanation
The crux of it is that the array feature is heavily used (a A
) to store the bitsums, of which the minimum and maximum are found, then 1
s and 0
s are outputted appropriately, with dumping of leading 0
s in two places.
$n Read in whole input as integers
I[( x] Convert each number to binary
d2% Get next least significant bit (modulo by 2)
i1+0a Get current value in array for that position
+ Add
i1+0A Put current value in array for that position
00ai` Get first value of array (bitsum length)
i` Greater than loop counter?
4&i00A If not, put loop counter there
2: Divide by 2
d) If 0, exit loop
00a Get first value of array (maximum length
1+ Add one
[i1+0a] Get bitsum values from array and push on stack
s Sort
0( Push a 0 (for dump) and start a while loop -- dumps leading 0s
x Dump top of stack
0g Get bottom of stack
d, Duplicate and <not> it
) Exit loop when top of stack is non-zero (i.e., the minimum)
r Reverse stack (so now it's [min, max, <stuff>])
I2-[x] Dump everything else
00a1+[ ] Get bitsum length and loop that many times
i1+0a Get bitsum value at current position
d Duplicate
0c= Copy front of stack and check for equality
$r Swap
1c= Copy next-to-front of stack and check for equality
+ Add (so it's 1 if it's equal to min or max, 0 otherwise)
0gx0gx Dump front two elements of stack (the min and max)
0(xd,) Dump leading 0s
0 Push 0 for conversion to decimal
I1-[ ] For each bit...
2*+ Multiply by 2 and add
N. Output as integer and stop
J, 31 30 24 23 21 bytes
+/(e.>./,<./@#~@)&.#:
This is a tacit, monadic verb that takes a list of decimal integers and returns their decimal biplex.
Thanks to @Zgarb for his suggestions, which saved 4 bytes directly and paved the way for 2 more!
Thanks to @randomra for golfing off 2 more bytes!
Test cases
biplex =: +/(e.>./,<./@#~@)&.#:
biplex ,1
1
biplex ,2
2
biplex 1 2 5
7
biplex ,4294967295
4294967295
biplex 2454267026 2863311530 3681400539
3817748707
biplex 2341103945 2969112506 1849078949 1430639189
3
How it works
&. Dual. Apply the verb to the right, the verb to the left,
and finally the inverse of the verb to the right.
#: Convert the input list from integer to base 2.
( @) Define an adverb, i.e., an operator that takes a verb as
its left argument.
+/ Call it with "reduce by sum". This calculates the sum of
the corresponding binary digits of all integers before
executing the remainder of the adverb's body, i.e, this:
#~ Replicate the sum N a total of N times, i.e., turn
0 1 2 3 into 1 2 2 3 3 3. This eliminates zeroes.
<./@ Calculate the minimum of the result.
>./ Calculate the maximum of the sums.
, Append; wrap both extrema into a list.
e. Check if each of the sums is in the list of extrema.
This yields 1 if yes and 0 if no.
(from &.) Convert from base 2 to integer.