int bit_reverse(int i, int bits) {
  unsigned int x = i;
  x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
  x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
  x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
  x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
  x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);
  return x >> (32 - bits);
}


x86 assembly (clang, -O0):
<bit_reverse>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xffff0000,%eax
shr    $0x10,%eax
mov    -0xc(%rbp),%ecx
and    $0xffff,%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff00ff00,%eax
shr    $0x8,%eax
mov    -0xc(%rbp),%ecx
and    $0xff00ff,%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xf0f0f0f0,%eax
shr    $0x4,%eax
mov    -0xc(%rbp),%ecx
and    $0xf0f0f0f,%ecx
shl    $0x4,%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xcccccccc,%eax
shr    $0x2,%eax
mov    -0xc(%rbp),%ecx
and    $0x33333333,%ecx
shl    $0x2,%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xaaaaaaaa,%eax
shr    $0x1,%eax
mov    -0xc(%rbp),%ecx
and    $0x55555555,%ecx
shl    $0x1,%ecx
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
sub    -0x8(%rbp),%ecx
shr    %cl,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_reverse>:
mov    %esi,%ecx
bswap  %edi
mov    %edi,%eax
and    $0xf0f0f0f,%eax
shl    $0x4,%eax
shr    $0x4,%edi
and    $0xf0f0f0f,%edi
or     %eax,%edi
mov    %edi,%eax
and    $0x33333333,%eax
shr    $0x2,%edi
and    $0x33333333,%edi
lea    (%rdi,%rax,4),%eax
mov    %eax,%edx
and    $0x55555555,%edx
shr    %eax
and    $0x55555555,%eax
lea    (%rax,%rdx,2),%eax
neg    %cl
shr    %cl,%eax
ret

LLVM IR (-O0):
define i32 @bit_reverse(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = and i32 %7, -65536
%9 = lshr i32 %8, 16
%10 = load i32, i32* %5
%11 = and i32 %10, 65535
%12 = shl i32 %11, 16
%13 = or i32 %9, %12
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = and i32 %14, -16711936
%16 = lshr i32 %15, 8
%17 = load i32, i32* %5
%18 = and i32 %17, 16711935
%19 = shl i32 %18, 8
%20 = or i32 %16, %19
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = and i32 %21, -252645136
%23 = lshr i32 %22, 4
%24 = load i32, i32* %5
%25 = and i32 %24, 252645135
%26 = shl i32 %25, 4
%27 = or i32 %23, %26
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = and i32 %28, -858993460
%30 = lshr i32 %29, 2
%31 = load i32, i32* %5
%32 = and i32 %31, 858993459
%33 = shl i32 %32, 2
%34 = or i32 %30, %33
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = and i32 %35, -1431655766
%37 = lshr i32 %36, 1
%38 = load i32, i32* %5
%39 = and i32 %38, 1431655765
%40 = shl i32 %39, 1
%41 = or i32 %37, %40
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = load i32, i32* %4
%44 = sub nsw i32 32, %43
%45 = lshr i32 %42, %44
ret i32 %45
}

LLVM IR (-O2):
define i32 @bit_reverse(i32 %0, i32 %1) {
%3 = tail call i32 @llvm.bitreverse.i32(i32 %0)
%4 = sub nsw i32 32, %1
%5 = lshr i32 %3, %4
ret i32 %5
}