输入值
输入是单个正整数 n
输出量
输出n
的最高有效位设置为0
。
测试用例
1 -> 0
2 -> 0
10 -> 2
16 -> 0
100 -> 36
267 -> 11
350 -> 94
500 -> 244
例如:350
in二进制为101011110
。设置其最高有效位(即最左边的1
位)以0
使其变成001011110
等于十进制整数94
的输出。这是OEIS A053645。
输入是单个正整数 n
输出n
的最高有效位设置为0
。
1 -> 0
2 -> 0
10 -> 2
16 -> 0
100 -> 36
267 -> 11
350 -> 94
500 -> 244
例如:350
in二进制为101011110
。设置其最高有效位(即最左边的1
位)以0
使其变成001011110
等于十进制整数94
的输出。这是OEIS A053645。
Answers:
i<=n
用n/i
的-1字节。这不是我的高尔夫,其他人试图将其编辑到您的帖子中,但是我将其回滚了,因为根据我们的社区规则,不接受高尔夫帖子的编辑。
BḊḄ
BḊḄ Main Link
B Convert to binary
Ḋ Dequeue; remove the first element
Ḅ Convert from binary
Ḅ
和Ḋ
两个字节的代码点?这会将整体大小更改为5个字节。
main(i){scanf("%d",&i);return i&~(1<<31-__builtin_clz(i));}
这个gcc答案仅使用整数按位和算术运算。这里没有对数!输入0可能会有问题,并且是完全不可移植的。
这是我在此网站上的第一个答案,因此,我希望能提供反馈和改进。我肯定对学习按位表达式很开心。
B0T(XB
多亏了Cinaski,节省了两个字节。切换到分配索引而不是引用索引的时间缩短了2个字节:)
% Grab input implicitly: 267
B % Convert to binary: [1 0 0 0 0 1 0 1 1]
0T( % Set the first value to 0: [0 0 0 0 0 1 0 1 1]
XB % Convert to decimal: 11
4L
而不是,则可以使用引用索引(也为6个字节)[2J]
。另一个有趣的6个字节:(tZlcW-
仅适用于MATLAB,不适用于TIO / Octave)
n->n^1<<(int)Math.log2(n)
将起作用,并且可能少于38个字节。如果这highestOneBit
不能正常工作,这是我的第二个想法(尚未经过测试)。出于好奇,您的解决方案是什么
n->n^1<<(int)(Math.log(n)/Math.log(2))
因为Math.log2
Java中不存在。; p只能Math.log
,Math.log10
并Math.loglp
可用。
Math.log2
确实不存在...我的坏。看到?highestOneBit
存在一种不错的方法(),但没有另一种(Math.log2
)。Java很奇怪;-)
ḋtḋ
说明:
-- implicit input, e.g. 350
ḋ -- convert number to list of binary digits (TNum -> [TNum]): [1,0,1,0,1,1,1,1,0]
t -- remove first element: [0,1,0,1,1,1,1,0]
ḋ -- convert list of binary digits to number ([TNum] -> TNum): 94
lambda n:n-2**len(bin(n))/8
lambda n:n-2**len(bin(n))/8 # Lambda Function: takes `n` as an argument
lambda n: # Declaration of Lambda Function
len(bin(n)) # Number of bits + 2
2** # 2 ** this ^
/8 # Divide by 8 because of the extra characters in the binary representation
n- # Subtract this from the original
2**len(bin(n))/8
也可以拼写1<<len(bin(n))-3
,然后它将同时在2和3中工作(不保存/添加字节)。
.slice`1`^0
时候.slice(1)^0
做得很好,哈哈
Tacit prefix function.
2⊥1↓2∘⊥⍣¯1
2∘⊥
… decode from base-2…
…⍣¯1
negative one time (i.e. encode in base-2)
1↓
drop the first bit
2⊥
decode from base-2
(You can omit destination register on add when same as source)
clz x1,x0
add x1,1
lsl x0,x1
lsr x0,x1
ret
shr
/shl
/ret
and wants instead something like lsr
/lsl
/bx lr
.
a^2sl
Explanation:
l Log base 2 of input.
s Cast ^ to integer (this is the position of the most significant bit.)
^2 Raise 2 to ^ (get the value of said bit)
a Subtract ^ from input
./-l
o@i
. Duplicate an implicit zero at the bottom of the stack. Does nothing.
/ Switch to Ordinal mode, move SE.
i Read all input as a string.
l Convert to lower case (does nothing, because the input doesn't contain letters).
i Try reading all input again, pushes an empty string.
/ Switch to Cardinal mode, move W.
. Duplicate. Since we're in Cardinal mode, this tries to duplicate an integer.
To get an integer, the empty string is discarded implicitly and the input is
converted to the integer value it represents. Therefore, at the end of this,
we get two copies of the integer value that was input.
l Clear lower bits. This sets all bits except the MSB to zero.
- Subtract. By subtracting the MSB from the input, we set it to zero. We could
also use XOR here.
/ Switch to Ordinal, move NW (and immediately reflect to SW).
o Implicitly convert the result to a string and print it.
/ Switch to Ordinal, move S.
@ Terminate the program.
^2p¢ÊÉ
^2p¢ÊÉ
¢ Get binary form of input
Ê Get length of that
É Subtract 1
2p Raise 2 to the power of that
^ XOR with the input
1
can fail: 4 bytes¢Ån2
Explanation: get input binary (¢
), slice off first char (Å
), parse as binary back to a number (n2
).
{2b()b}
Explanation:
{ } Block: 267
2b Binary: [1 0 0 0 0 1 0 1 1]
( Pop: [0 0 0 0 1 0 1 1] 1
) Increment: [0 0 0 0 1 0 1 1] 2
b Base convert: 11
Reuse the MSB (which is always 1) to avoid having to delete it; the equivalent without that trick would be {2b1>2b}
or {2b(;2b}
.
^(^1|\1\1)*1
Input and output in unary (the test suite includes conversion from and to decimal for convenience).
This is quite easy to do in unary. All we want to do is delete the largest power of 2 from the input. We can match a power of 2 with some forward references. It's actually easier to match values of the form 2n-1, so we'll do that and match one 1 separately:
^(^1|\1\1)*1
The group 1
either matches a single 1
at the beginning to kick things off, or it matches twice what it did on the last iteration. So it matches 1
, then 2
, then 4
and so on. Since these get added up, we're always one short of a power of 2, which we fix with the 1
at the end.
Due the trailing linefeed, the match is simply removed from the input.
function(x)x-2^(log2(x)%/%1)
Easiest to calculate the most significant bit via 2 ^ floor(log2(x))
rather than carry out base conversions, which are quite verbose in R
n->n-2^logint(n,2)
Alternate solution:
n->n-2^exponent(n)
n->n-2^logint(n,2)
? The second one is not supported in my version of PARI/GP, nor in the version used by tio.run. Is that a new function?
exponent
was added 5 days ago, compared to this challenge which was added yesterday. :)
(!1)
x!y|2*y>x=x-y|z<-2*y=x!z
-3 bytes thanks to @Laikoni
f x=last[x-2^i|i<-[0..x],2^i<=x]
f=
in the first variant. Additionally z<-2*y=x!z
saves a byte: Try it online!
-5
bytes thanks to @IanM_Matrix1
=BIN2DEC(MID(DEC2BIN(A1),2,99))
Nothing interesting.
10
显然给0
:d