Hot answers tagged

52

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 or doubles as arguments (in xmm0 and xmm1) and returns a float or double (in xmm0). That matches the calling conventions of both Windows x64 and the x86-64 SysV ABI, and works for floats as well as doubles. (They're passed / ...


50

Jelly, 7 6 bytes _/ị“ḃ» Typing on phone. Will add description. (1,0) goes to A, (0,1) to B, and (0,0) to C. Arrays in Jelly are 1-based, and the indexing function ị works cyclically. Therefore, we can just fold subtraction over the input. _ [vectorized] subtraction _/ Fold subtraction over the input “ḃ» "ABC" ...


41

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 higher processor with lzcnt instruction. Try it online!


36

x86 machine code: 7 bytes 66 0F 3A 44 C1 00 C3 pclmulqdq xmm0, xmm1, 0 \ ret Only two instructions. pclmulqdq does the heavy lifting, it literally implements that type of xor-multiplication. ret to make it a callable function, hopefully satisfying the requirement of "outputting" the result (in the return value, xmm0). Putting integer arguments in xmm args ...


36

Pyth, 40 36 31 30 bytes Ju.|G^2slHxMf>FT.:Q2Z|tSIxRJQJ Try it online: Demonstration or Test Suite Each of the big test-cases finishes in a couple of seconds. Explanation: First I'll explain the method and why it works. I'll do this with the example list: [7, 2, 13, 9]. The first two numbers are already wrong (7 > 2). We want to xor with some ...


32

Python 2.7, 44 -> 36 bytes lambda x:x/4&42|x*2&128|x*4&84|x/2&1


29

x86-64 assembly, 5 4 bytes 0: 97 xchg %eax,%edi 1: d1 c0 rol %eax 3: c3 retq A function using the C calling convention that bitwise rotates its argument left by 1 bit.


27

J (11) (\:+/"1@#:) This is a function that takes a list: (\:+/"1@#:) 15342 28943 16375 3944 11746 825 32425 28436 21826 15752 19944 16375 15342 32425 28943 11746 28436 19944 3944 15752 825 21826 If you want to give it a name, it costs one extra character: f=:\:+/"1@#: f 15342 28943 16375 3944 11746 825 32425 28436 21826 15752 19944 16375 ...


24

MMIX assembly (28 Bytes) 64 bit numbers rbit: SETH $1,#0102 # load matrix in 16-byte steps ORMH $1,#0408 ORML $1,#1020 ORL $1,#4080 MOR $0,$1,$0 # multiplication 1 MOR $0,$0,$1 # multiplication 2 POP 1,0 # return This assembles to: rbit: E0010102 # SETH $1,#0102 E9010408 # ORMH $1,#0408 EA011020 # ORML $1,#...


24

JavaScript (ES6), 41 40 36 34 bytes Saved 4 bytes thanks to @ThePirateBay f=x=>(k=x&x/2)?f(k):Math.log2(x)|0 Test cases f=x=>(k=x&x/2)?f(k):Math.log2(x)|0 console.log(f(0)) console.log(f(142)) console.log(f(48)) console.log(f(750)) How? General case x > 0 We recursively AND the input x with x / 2 which progressively ...


24

Hexagony, 78 70 bytes 2"1"\.}/{}A=<\?>(<$\*}[_(A\".{}."&.'\&=/.."!=\2'%<..(@.>._.\=\{}:"<><$ Try it online! Isn't this challenge too trivial for a practical language? ;) side length 6. I can't fit it in a side length 5 hexagon. Explanation


21

Mathematica, 133 bytes FromCharacterCode@Nest[BlockMap[If[#>126,#~Mod~127+32,#]&[BitXor[#,#2]~BitOr~#3]&@@#&,ArrayPad[#,1,32],3,1]&,ToCharacterCode@#,#2+1]& It would be nice to make a CellularAutomaton[] solution work, but I kept coming up short. Anyone? Edit: some pretty pictures (click to enlarge) plotCA[str_, n_] := ArrayPlot[...


21

Jelly, 10 8 bytes Ba\ÐƤṀċ¬ Try it online! How it works Ba\ÐƤṀċ¬ Main link. Argument: n B Binary; convert n to base 2. ÐƤ Apply the link to the left to all postfixes of the binary array. a\ Cumulatively reduce by logical AND. For example, the array [1, 0, 1, 1, 1, 0, 1, 1, 1, 0] becomes the ...


17

JavaScript, 39 Update: Now shorter than Ruby. x.sort(q=(x,y)=>!x|-!y||q(x&x-1,y&y-1)) 40 x.sort(q=(x,y)=>x&&y?q(x&x-1,y&y-1):x-y) Explanation: q is a recursive function. If x or y are 0, it returns x-y (a negative number if x is zero or a positive number if y is zero). Otherwise it removes the lowest bit (x&x-1) from x ...


17

Python, 32 bytes lambda x:("-~"*abs(x))[x<0:]+"0" Anonymous lambda function. Given an integer x writes "-~" abs(x) times and removes the first char if x is negative, then a zero is added to the end.


16

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 ?. Explanation: r #Read a character e #Weave it w #Display it This so feels like cheating.


16

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 the input: if it's less than 0, flip it and add a ~ to the string; if it's more than 0, negate it and add a - to the string; if it's exactly 0, return 0. Takes advantage of this pattern: 0 ...


15

Ruby 41 f=->a{a.sort_by{|n|-n.to_s(2).count(?1)}} Test: a = [28943, 825, 11746, 16375, 32425, 19944, 21826, 15752, 15342, 3944, 28436]; f[a] => [16375, 15342, 32425, 11746, 28436, 28943, 19944, 15752, 3944, 21826, 825]


15

Pyth, 36 35 bytes u%@[t_G/G2yGi_jGJ16JG)x"!><@"H256z0 Test harness The internal representation of the accumulator is an integer. This integer is mod-ed by 256 on each iteration, as desired. The operations performed are -G-1, G/2, G*2 and G converted to base 16, reversed, and converted back to base 10, where G is the accumulator. I missed the line ...


15

Ruby 2, 119 ->a,*o{a.each_cons(2){|x,y|x==y||o[i=(x^y).bit_length-1]==1-(o[i]=x[i])&&(return-1)};(o.map(&:to_i).reverse*'').to_i 2} Runs in 42 milliseconds on the large test cases. Ungolfed: def first_differing_bit(a,b) (a^b).bit_length - 1 end def xort(ary) required_bits = [] ary.each_cons(2) do |a,b| i = first_differing_bit(a,...


15

Pyth, 18 17 bytes iR2c.[t.B+C1z\0QQ Thanks to @lirtosiast for a byte! z get input +C1 prepend a 0x01 to prevent leading zeroes from disappearing .B convert to binary string t remove the leading 1 from ^^ .[ \0Q pad right with zeroes to multiple of second input c Q ...


14

Z80, 11 bytes B7 CB 32 30 01 B3 C8 CB 23 18 F6 The code is called as a function. a and b are in D and E (the order doesn't matter) and the answer is stored in A when the code returns (there are no I/O functions). B7 XOR A // A^=A (A=0) CB 32 SRL D // CARRY = lsb(D), D>>=1, ZERO = D==0 30 01 JR NC, 1 // jump 1 byte if not ...


14

05AB1E, 5 4 bytes I proudly present to you, 05AB1E. Although it is very short, it is probably very bad at long challenges. Thanks to ETHproductions for shaving off 1 byte :) $Fx^ Explanation: $ # Pushes 1 and input F # Pops x, creates a for-loop in range(0, x) x # Pops x, pushes x and 2x ^ # Bitwise XOR on the last two elements ...


14

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^2, \ldots, n^2\right]\$ (which is far more than enough), XORs it with \$n\$, and then takes either the number of 1s in its binary representation if the number of bits is less than or equal to \$\...


13

C, 96 Assuming ASCII (or compatible) input: a;main(c){while(c=getchar()+1)a=(c^34?c^61?c^63?c^65?a:a*257/16:a/2:a*2:~a)&255;printf("%u",a);} Tidier: a; main(c){ while(c=getchar()+1) a=(c^34? c^61? c^63? c^65? a : a*257/16 : a/2 :a*2:~a )&255; printf("%u",...


13

Jelly, 13 bytes 1;ḅ256æ«BḊsḄṖ This takes the input as a list of integers. Try it online! How it works 1;ḅ256æ«BḊsḄṖ Main link. Arguments: A (list), n (integer) 1; Prepend 1 to A. ḅ256 Convert from base 256 to integer. æ« Bitshift the result n units to the left. B Convert to binary. Ḋ Discard ...


13

Pyth, 6 bytes .Oml.B Try it online here. .Oml.BdUQ Filling in implict vars .O Average of list m UQ Map over [0..input) l Length of .B Binary string representation of int d Lambda var


13

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 for an explanation of how it works. The validation is quite slow (as in, running time measured in hours): I recommend using PyPy or Cython. All layers use ReLU (\$\alpha \to \max(0, \alpha)\$) ...


12

Pari/GP, 27 bytes n->lift(Mod(x+1,2)^n)%(x-2) The function takes the n-th power of the polynomial x+1 in the ring F2[x], lifts it to Z[x], and then evaluates it at 2. Try it online!


12

MATL, 10 bytes 0i2$:tXgZ~ The compiler (and in particular this program) now seems to work in Octave, although it still needs some refinement. You can provisionally use this GitHub commit. Edit (Mar 30 '16): Try it online! Example >> matl 0i2$:tXgZ~ > 9 0 1 2 3 4 5 6 7 8 9 1 0 3 2 5 4 7 6 9 8 2 3 0 1 6 7 4 5 10 11 3 2 ...


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