Skip to main content
57 votes

Floating Point XOR

x86-64 machine code, 4 bytes 0f 57 c1 c3 In assembly: xorps xmm0, xmm1 ret This is a callable function that takes two floats ...
user555045's user avatar
  • 1,419
42 votes

Find the number of leading zeroes in a 64-bit integer

x86_64 machine language on Linux, 6 bytes 0: f3 48 0f bd c7 lzcnt %rdi,%rax 5: c3 ret Requires Haswell or K10 or ...
ceilingcat's user avatar
  • 7,605
33 votes

Golf bit weaving

Python 2.7, 44 -> 36 bytes lambda x:x/4&42|x*2&128|x*4&84|x/2&1
Arfie's user avatar
  • 1,241
29 votes

Different number, same weight

x86-64 assembly, 5 4 bytes 0: 97 xchg %eax,%edi 1: d1 c0 rol %eax 3: c3 retq A ...
Anders Kaseorg's user avatar
25 votes

Calculate the longest series of 1's in an integer's binary value

JavaScript (ES6),  33  32 bytes f=x=>x&(k=x>>1)?f(x&k):k&&1+f(k) Try it online! JavaScript (ES6),  41 40 36  34 bytes Saved 4 bytes ...
Arnauld's user avatar
  • 193k
25 votes

Find the number of leading zeroes in a 64-bit integer

Hexagony, 78 70 bytes 2"1"\.}/{}A=<\?>(<$\*}[_(A\".{}."&.'\&=/.."!=\2'%<..(@.>._.\=\{}:"<><$ Try it online! Isn't this challenge ...
DELETE_ME's user avatar
  • 17.3k
22 votes
Accepted

Calculate the longest series of 1's in an integer's binary value

Jelly, 10 8 bytes Ba\ÐƤṀċ¬ Try it online! How it works ...
Dennis's user avatar
  • 210k
20 votes

Compress 8 bits to 6 when certain bit patterns will never occur

Python, 60 bytes l=[n^170for n in range(256)if n&n*2<1] l.index,l.__getitem__ Try it online! l.index compresses and <...
xnor's user avatar
  • 145k
17 votes

Bitflip and negate

Python, 32 bytes lambda x:("-~"*abs(x))[x<0:]+"0" Anonymous lambda function. Given an integer x writes "-~" abs(x) times and ...
KarlKastor's user avatar
  • 2,392
16 votes

Bitflip and negate

JavaScript (ES6), 33 31 bytes f=x=>x<0?"~"+f(~x):x&&"-"+f(-x) Recursion < built-ins < loops (at least in this case). Basically unevaluates ...
ETHproductions's user avatar
16 votes

XOR of independent Bernoulli variables

Haskell, 23 bytes foldr1$ \x p->x+p-2*p*x Try it online! If you have two events with probabilities x and ...
xnor's user avatar
  • 145k
15 votes

Golf bit weaving

Evil, 3 characters rew Try it online! Input is in base 256, (e.g. ASCII), e.g. to enter the digit 63, enter ASCII 63 which is ?....
DJMcMayhem's user avatar
  • 59.3k
14 votes

Toggle some bits and get a square

Ruby, 74 bytes ->n{(1..n).map{|x|a=(n^x*x).to_s 2;a.size>Math.log2(n)?n:a.count(?1)}.min} Try it online! This simply generates the sequence \$\left[1^2, 2^...
Doorknob's user avatar
  • 71.5k
14 votes

Can a neural network recognize primes?

Trial division: score 59407, 6243 layers, 16478 neurons in total Given as a Python program which generates and validates the net. See the comments in trial_division...
Peter Taylor's user avatar
  • 43.1k
13 votes
Accepted

Mean bits: an average challenge

Pyth, 6 bytes .Oml.B Try it online here. ...
Maltysen's user avatar
  • 25.7k
13 votes

Calculate the longest series of 1's in an integer's binary value

x86/x86-64 machine code, 14 bytes (TODO: 13 bytes with input in EAX, return value in EDX). Using @Arnauld's algorithm of x &= x>>1 and taking the highest ...
Peter Cordes's user avatar
  • 4,837
12 votes

Compute CRC32 Hash

Intel x86, 34 30 29 27 bytes Takes the address of the zero-terminated string in ESI, and returns the CRC in EBX: ...
Mark Adler's user avatar
12 votes
Accepted

"Bit-borrow" two numbers

Jelly, 11 bytes ~1¦&N$^µ</¿ Try it online! or verify all test cases. Background We can extract the last set bit of an integer n as follows. n + 1 ...
Dennis's user avatar
  • 210k
12 votes

Folding Numbers

Jelly, 9 bytes Bœs2µḢ^UȦ Try it online! or verify all test cases. How it works ...
Dennis's user avatar
  • 210k
12 votes

Alternating bit smearing

Python 3, 20 bytes lambda n:(n&-n)//3+n Try it online! Explanation Take 192 as an example. Its binary form is ...
Leaky Nun's user avatar
  • 49.8k
12 votes

Find the number of leading zeroes in a 64-bit integer

Python, 31 bytes lambda n:67-len(bin(-n))&~n>>64 Try it online! The expresson is the bitwise & of two parts: <...
xnor's user avatar
  • 145k
12 votes

Decode a variable-length quantity

APL (dzaima/APL), 8 bytes 128(⊣⊥|) Try it online! How: ...
J. Sallé's user avatar
  • 3,453
12 votes

Floating Point XOR

C++ (gcc), 74 32 bytes #define f(x,y)*(int*)x^=*(int*)y Try it online! I haven’t previously golfed in C++ so am grateful to all those who helped halve the size ...
Nick Kennedy's user avatar
  • 21.1k
12 votes

Is it a valid ARM immediate value?

Rust, 52 49 bytes |n|(0..16).any(|i|(n<<33^n<<32^n*2^n)<<i*2>>41<1) Attempt This Online! A closure that takes an unsigned 32-bit value ...
Bubbler's user avatar
  • 77k
11 votes

Golf bit weaving

CJam, 15 12 bytes Thanks to FryAmTheEggman for saving 3 bytes. l8Te[m!6532= Input in base 2. Output also in base 2, padded to 8 bits with zeros. Test it here. ...
Martin Ender's user avatar
11 votes

Bitflip and negate

Pyth, 14 13 12 Bytes _<>0Q+0sm"~- -2 Bytes thanks to @StevenH. test suite Decided to try out Pyth, so i translated my python answer to it. Any help ...
KarlKastor's user avatar
  • 2,392
11 votes

Find the number of leading zeroes in a 64-bit integer

C (gcc), 14 bytes __builtin_clzl Works fine on tio C (gcc), 35 29 bytes f(long n){n=n<0?0:f(n-~n)+1;} Try it online! ...
l4m2's user avatar
  • 24.5k
11 votes

Inverting string-based binary number in one line in Python

Python 3,  51 44 43  41 bytes -7 thanks to loopy walt's suggestion to use *open. -1 thanks to xnor's suggestion to replace ...
Jonathan Allan's user avatar
11 votes
Accepted

Count the number of disjoint 11 blocks

5 ops E = int('01'*32, 2) # ...01010101 def f(n): r = (n ^ E) + E # 2 ops t = n & (E ^ r) # 2 ops return popcount(t) # 1 op Try it online! 6 ops <...
xnor's user avatar
  • 145k

Only top scored, non community-wiki answers of a minimum length are eligible