Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0 -> 0
- 1 -> 2
- 9 -> 6
- 85 -> 170
- 220 -> 236
- 1827 -> 2835
- 47525 -> 30298
- b4d2UFḄ
- b4d2UFḄ Main link. Argument: n (integer)
- b4 Convert n to base 4.
- d2 Divmod each quaternary digit x by 2, yielding [x : 2, x % 2].
- U Upend; reverse each pair, yielding [x % 2, x : 2].
- F Flatten the 2D list of binary digits.
- Ḅ Convert from binary to integer.
- n = o + 2*e
- n = ...hgfedcba
- o = ...0g0e0c0a
- 2*e = ...h0f0d0b0
- s = 2*o + e
- s = ...ghefcdab
- 2*o = ...g0e0c0a0
- e = ...0h0f0d0b
- s=2*(n-2*e)+e = 2*n-3*e
- n/2 = ...hgfedcb
- M = ...1010101
- n/2 & M = ...h0f0d0b = e
- ri4b4e!2=f=4b
- ri e# Read input and convert to integer.
- 4b e# Get base-4 digits.
- 4e! e# Push all permutations of [0 1 2 3].
- 2= e# Select the third one which happens to be [0 2 1 3].
- f= e# For each base-4 digit select the value at that position in the previous
- e# list, which swaps 1s and 2s.
- 4b e# Convert back from base 4.
- iXjQ4S2)4
- jQ4 Convert the input (Q) to base 4.
- X S2) Translate [1, 2] to [2, 1].
- i 4 COnvert from base 4 to integer.
- (n,m=0x55555555)=>n*2&~m|n/2&m
- (n,m=0x55555555)=>(n*2&~m|n/2&m)>>>0
- n=>parseInt(n.toString(4).replace(/1|2/g,n=>3-n),4)
- n4÷axxn4÷xxe+3120
- n4÷axxn4÷xxe+3120
- 0 a(0) = 0
- 2 a(1) = 2
- 1 a(2) = 1
- 3 a(3) = 3
- n push n (input)
- 4÷ integer-divide by 4
- a a(n/4)
- xx double twice; multiply by 4
- now we have 4a(n/4)
- n push n (input)
- 4÷xx integer-divide by 4 and then multiply by 4
- since there is no modulo currently, n%4
- is built as n-(n/4*4)
- e we should have done a(n-(n/4*4)), but this
- is a shortcut for a(n-x) where x is the top
- of stack. Therefore, we now have a(n-n/4*4)
- which is a(n%4).
- + add.
- BP2eP1ePXB
- B % Take input implicitly. Convert to binary array
- P % Flip
- 2e % Convert to two-row 2D array, padding with a trailing zero if needed.
- % Because of the previous flip, this really corresponds to a leading zero
- P % Flip each column. This corresponds to swapping the bits
- 1e % Reshape into a row
- P % Flip, to undo the initial flipping
- XB % Convert from binary array to number. Display implicitly
- 0000000000000040 <onemask_even>:
- 40: 89 f8 mov eax,edi
- 42: 25 55 55 55 55 and eax,0x55555555
- 47: 29 c7 sub edi,eax
- 49: d1 ef shr edi,1
- 4b: 8d 04 47 lea eax,[rdi+rax*2]
- 4e: c3 ret
- 4f: <end>
- unsigned onemask_even(unsigned x) {
- unsigned emask = ~0U/3;
- unsigned e = (x & emask);
- return e*2 + ((x - e) >> 1);
- }
- # since 0x55 has its low bit set, shifting it out the top of RAX will set CF
- 0000000000000000 <swap_bitpairs64>:
- 0: 31 c0 xor eax,eax ; old garbage in rax could end the loop early
- 0000000000000002 <swap_bitpairs64.loop>:
- 2: 48 c1 e0 08 shl rax,0x8
- 6: b0 55 mov al,0x55 ; set the low byte
- 8: 73 f8 jnc 2 <swap_bitpairs64.loop> ; loop until CF is set
- 000000000000000a <swap_bitpairs64.rest_of_function_as_normal>:
- # 10 bytes, same as mov rax, 0x5555555555555555
- # rax = 0x5555...
- a: 48 21 f8 and rax,rdi
- ...
- <<<$[`tr 12 21<<<$[[#4]$1]`]
- $1 input (first command line argument)
- $[ ] arithmetic expansion
- [#4] output in base 4
- <<< pass the result of this to...
- tr the `tr' command
- 12 21 and replace 1s with 2s, 2s with 1s
- ` ` evaluate the result...
- $[ ] in another arithmetic expansion, to convert back
- to base 10
- <<< output the result on STDOUT
- f(x){return(x&~0U/3)*3+x>>1;} // 30 bit version, see below
- // less golfed:
- f(x){return ((x & 0x55555555)*3 + x) >>1;} // >> is lower precedence than +
- g(x){return 2*x-3*(x/2U&~0U/3);} // safe 32bit version, works for all x
- n=>+('0b'+/(..)+$/.exec('0'+n.toString`2`)[0].split``.reduce((p,c)=>p.length-1?[p.join(c)]:[p[0],c],[''])[0],2)
- n=>+('0b'+ //parse as binary literal
- /(..)+$/.exec('0'+n.toString`2`)[0] //convert to binary string with an even number of digits
- .split`` //convert to array
- .reduce((p,c)=>p.length-1?[p.join(c)]:[p[0],c],[''])
- //swap all numbers
- )
- T`12`21
- T`12`21
- 4B12‡4ö
- 4B - Take input and convert to base 4.
- 12Â - Push 12 bifurcated.
- ‡ - Transliterate [1, 2] to [2, 1].
- 4ö - Convert to base 10.
- 4#.0 2 1 3{~4#.^:_1]
- >> f =: 4#.0 2 1 3{~4#.^:_1]
- >> f 85
- << 170
- to_base =: 4 #.^:_1 ]
- transpose =: 0 2 1 3 {~ to_base
- from_base =: 4 #. transpose
- Fold[3#+##&,#~IntegerDigits~4/.{1->2,2->1}]&
- 4@¡"21""12"(t4@¿
- 4@¡"21""12"(t4@¿
- 4@¡ base 4 representation of n
- "21""12"(t translate (swap 1s and 2s)
- 4@¿ base 4 to decimal
- ([:,_2|.,&0)&.(|.@#:)
- f =: ([:,_2|.,&0)&.(|.@#:)
- (,.f"0) 0 1 9 85 220 1827 47525
- 0 0
- 1 2
- 9 6
- 85 170
- 220 236
- 1827 2835
- 47525 30298
- ([:,_2|.,&0)&.(|.@#:) Input: n
- #: Get the value as a list of base 2 digits
- |.@ Reverse it
- ( )&. Apply to the list of base 2 digits
- ,&0 Append a zero to the end of the list
- _2 Split the list into nonoverlapping sublists of size 2
- |. Reverse each sublist
- [:, Flatten the list of sublists into a list
- &.( ) Apply the inverse of (reversed base 2 digits)
- to convert back to a number and return it
- n=x2b(d2x(arg(1)))
- o=0
- do while n>''
- parse var n a+1 b+1 n
- o=o||b||a
- end
- say x2d(b2x(o))
Add Comment
Please, Sign In to add comment