unsigned long bitsReverse64(unsigned long n) {
  n = (((n) >> (32)) & (0x00000000FFFFFFFFUL)) |
      (((n) & (0x00000000FFFFFFFFUL)) << (32));
  ;
  n = (((n) >> (16)) & (0x0000FFFF0000FFFFUL)) |
      (((n) & (0x0000FFFF0000FFFFUL)) << (16));
  ;
  n = (((n) >> (8)) & (0x00FF00FF00FF00FFUL)) |
      (((n) & (0x00FF00FF00FF00FFUL)) << (8));
  ;
  n = (((n) >> (4)) & (0x0F0F0F0F0F0F0F0FUL)) |
      (((n) & (0x0F0F0F0F0F0F0F0FUL)) << (4));
  ;
  n = (((n) >> (2)) & (0x3333333333333333UL)) |
      (((n) & (0x3333333333333333UL)) << (2));
  ;
  n = (((n) >> (1)) & (0x5555555555555555UL)) |
      (((n) & (0x5555555555555555UL)) << (1));
  ;
  return n;
}


x86 assembly (clang, -O0):
<bitsReverse64>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x20,%rax
movabs $0xffffffff,%rcx
and    %rcx,%rax
movabs $0xffffffff,%rcx
and    -0x8(%rbp),%rcx
shl    $0x20,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x10,%rax
movabs $0xffff0000ffff,%rcx
and    %rcx,%rax
movabs $0xffff0000ffff,%rcx
and    -0x8(%rbp),%rcx
shl    $0x10,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x8,%rax
movabs $0xff00ff00ff00ff,%rcx
and    %rcx,%rax
movabs $0xff00ff00ff00ff,%rcx
and    -0x8(%rbp),%rcx
shl    $0x8,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x4,%rax
movabs $0xf0f0f0f0f0f0f0f,%rcx
0f 0f 0f
and    %rcx,%rax
movabs $0xf0f0f0f0f0f0f0f,%rcx
0f 0f 0f
and    -0x8(%rbp),%rcx
shl    $0x4,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x2,%rax
movabs $0x3333333333333333,%rcx
33 33 33
and    %rcx,%rax
movabs $0x3333333333333333,%rcx
33 33 33
and    -0x8(%rbp),%rcx
shl    $0x2,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
shr    $0x1,%rax
movabs $0x5555555555555555,%rcx
55 55 55
and    %rcx,%rax
movabs $0x5555555555555555,%rcx
55 55 55
and    -0x8(%rbp),%rcx
shl    $0x1,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitsReverse64>:
bswap  %rdi
mov    %rdi,%rax
shr    $0x4,%rax
movabs $0xf0f0f0f0f0f0f0f,%rcx
0f 0f 0f
and    %rcx,%rax
and    %rcx,%rdi
shl    $0x4,%rdi
or     %rax,%rdi
movabs $0x3333333333333333,%rax
33 33 33
mov    %rdi,%rcx
and    %rax,%rcx
shr    $0x2,%rdi
and    %rax,%rdi
lea    (%rdi,%rcx,4),%rax
movabs $0x5555555555555555,%rcx
55 55 55
mov    %rax,%rdx
and    %rcx,%rdx
shr    %rax
and    %rcx,%rax
lea    (%rax,%rdx,2),%rax
ret

LLVM IR (-O0):
define i64 @bitsReverse64(i64 %0) {
%2 = alloca i64
store i64 %0, i64* %2
%3 = load i64, i64* %2
%4 = lshr i64 %3, 32
%5 = and i64 %4, 4294967295
%6 = load i64, i64* %2
%7 = and i64 %6, 4294967295
%8 = shl i64 %7, 32
%9 = or i64 %5, %8
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = lshr i64 %10, 16
%12 = and i64 %11, 281470681808895
%13 = load i64, i64* %2
%14 = and i64 %13, 281470681808895
%15 = shl i64 %14, 16
%16 = or i64 %12, %15
store i64 %16, i64* %2
%17 = load i64, i64* %2
%18 = lshr i64 %17, 8
%19 = and i64 %18, 71777214294589695
%20 = load i64, i64* %2
%21 = and i64 %20, 71777214294589695
%22 = shl i64 %21, 8
%23 = or i64 %19, %22
store i64 %23, i64* %2
%24 = load i64, i64* %2
%25 = lshr i64 %24, 4
%26 = and i64 %25, 1085102592571150095
%27 = load i64, i64* %2
%28 = and i64 %27, 1085102592571150095
%29 = shl i64 %28, 4
%30 = or i64 %26, %29
store i64 %30, i64* %2
%31 = load i64, i64* %2
%32 = lshr i64 %31, 2
%33 = and i64 %32, 3689348814741910323
%34 = load i64, i64* %2
%35 = and i64 %34, 3689348814741910323
%36 = shl i64 %35, 2
%37 = or i64 %33, %36
store i64 %37, i64* %2
%38 = load i64, i64* %2
%39 = lshr i64 %38, 1
%40 = and i64 %39, 6148914691236517205
%41 = load i64, i64* %2
%42 = and i64 %41, 6148914691236517205
%43 = shl i64 %42, 1
%44 = or i64 %40, %43
store i64 %44, i64* %2
%45 = load i64, i64* %2
ret i64 %45
}

LLVM IR (-O2):
define i64 @bitsReverse64(i64 %0) {
%2 = tail call i64 @llvm.bitreverse.i64(i64 %0)
ret i64 %2
}