###Original attempt - (削除) 120 98 (削除ここまで) 95 bytes
Original attempt - (削除) 120 98 (削除ここまで) 95 bytes
###Original attempt - (削除) 120 98 (削除ここまで) 95 bytes
Original attempt - (削除) 120 98 (削除ここまで) 95 bytes
Octave, 81(削除) 81 (削除ここまで) 80 bytes
@(x)max(bin2dec((d=@dec2bin)dec2bin(x*(c=2.^(0:(b=nnz(db=nextpow2(x)x+1)-8))))(:,[1:b b+9:end]))'./c)
@(x)max( )
bin2dec( )'./c
(:,[1:b b+9:end])
(d=@dec2bin)dec2bin(x*( ))
))
c=2.^(0: )
)
(b=nnz(db=nextpow2(x)x+1)-8)
On the sixth line the number of groups is calculated by convertingfinding the input to binary and countingexponent of the lengthnext power of two larger than the resulting stringinput (number of bits in input number), and subtracting 7 as we are removing 8 bits from each group - the resulting number is stored in b for later.
- Save 1 byte by using
nextpow2(x+1)rather thannnz(bin2dec(x))
Octave, 81 bytes
@(x)max(bin2dec((d=@dec2bin)(x*(c=2.^(0:(b=nnz(d(x))-8))))(:,[1:b b+9:end]))'./c)
@(x)max( )
bin2dec( )'./c
(:,[1:b b+9:end])
(d=@dec2bin)(x*( ))
c=2.^(0: )
(b=nnz(d(x))-8)
On the sixth line the number of groups is calculated by converting the input to binary and counting the length of the resulting string, subtracting 7 as we are removing 8 bits from each group - the resulting number is stored in b for later.
Octave, (削除) 81 (削除ここまで) 80 bytes
@(x)max(bin2dec(dec2bin(x*(c=2.^(0:(b=nextpow2(x+1)-8))))(:,[1:b b+9:end]))'./c)
@(x)max( )
bin2dec( )'./c
(:,[1:b b+9:end])
dec2bin(x*( ))
c=2.^(0: )
(b=nextpow2(x+1)-8)
On the sixth line the number of groups is calculated by finding the exponent of the next power of two larger than the input (number of bits in input number), and subtracting 7 as we are removing 8 bits from each group - the resulting number is stored in b for later.
- Save 1 byte by using
nextpow2(x+1)rather thannnz(bin2dec(x))
Octave, (削除) 120 98 (削除ここまで) 9581 bytes
@(x)max(bin2dec((d=@dec2bin)(x*(c=2.^(0:(b=nnz(d(x))-8))))(:,[1:b b+9:end]))'./c)
This is a different solution to my original attempt, saving a further 14 bytes.
The code is broken down as follows:
@(x)max( )
bin2dec( )'./c
(:,[1:b b+9:end])
(d=@dec2bin)(x*( ))
c=2.^(0: )
(b=nnz(d(x))-8)
On the sixth line the number of groups is calculated by converting the input to binary and counting the length of the resulting string, subtracting 7 as we are removing 8 bits from each group - the resulting number is stored in b for later.
We then calculate an array of powers of two on the fifth line which is large enough for all the possible groups that can be removed. We save this in variable c for later.
On the next line up (forth line), we multiply the input by the array of powers of two (essentially bit shifting each number up), and convert the result to binary. If we take the example of 7831, this results in a 2D array containing:
000001111010010111
000011110100101110
000111101001011100
001111010010111000
011110100101110000
111101001011100000
If we then chop out the centre 8 bits, that is equivalent to removing each of the groups of 8 bits. This is done by the third line.
The resulting array is converted back to decimal on the second line. We also have to divide by c to undo the scaling that was done to each group initially.
Finally on the first line an anonymous function is declared, and the maximum value from all groups is calculated.
###Original attempt - (削除) 120 98 (削除ここまで) 95 bytes
Octave, (削除) 120 98 (削除ここまで) 95 bytes
Octave, 81 bytes
@(x)max(bin2dec((d=@dec2bin)(x*(c=2.^(0:(b=nnz(d(x))-8))))(:,[1:b b+9:end]))'./c)
This is a different solution to my original attempt, saving a further 14 bytes.
The code is broken down as follows:
@(x)max( )
bin2dec( )'./c
(:,[1:b b+9:end])
(d=@dec2bin)(x*( ))
c=2.^(0: )
(b=nnz(d(x))-8)
On the sixth line the number of groups is calculated by converting the input to binary and counting the length of the resulting string, subtracting 7 as we are removing 8 bits from each group - the resulting number is stored in b for later.
We then calculate an array of powers of two on the fifth line which is large enough for all the possible groups that can be removed. We save this in variable c for later.
On the next line up (forth line), we multiply the input by the array of powers of two (essentially bit shifting each number up), and convert the result to binary. If we take the example of 7831, this results in a 2D array containing:
000001111010010111
000011110100101110
000111101001011100
001111010010111000
011110100101110000
111101001011100000
If we then chop out the centre 8 bits, that is equivalent to removing each of the groups of 8 bits. This is done by the third line.
The resulting array is converted back to decimal on the second line. We also have to divide by c to undo the scaling that was done to each group initially.
Finally on the first line an anonymous function is declared, and the maximum value from all groups is calculated.
###Original attempt - (削除) 120 98 (削除ここまで) 95 bytes