fork download
  1. #include <stdint.h>
  2. #include <stdio.h>
  3.  
  4. void format_binary(uint64_t x, char *buf)
  5. {
  6. int i = 0;
  7. while(x){
  8. char b = x % 2;
  9. buf[i] = b + '0';
  10. x /= 2;
  11. i++;
  12. }
  13. int m = i / 2;
  14. for(int j = 0; j < m; j++){
  15. char tmp = buf[j];
  16. buf[j] = buf[i - 1 - j];
  17. buf[i - 1 - j] = tmp;
  18. }
  19. buf[i] = '\0';
  20. }
  21.  
  22. static inline uint64_t interleave64(uint32_t xlo, uint32_t ylo) {
  23. static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,
  24. 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
  25. 0x0000FFFF0000FFFFULL};
  26. char bf[200];
  27. for(int i = 0; i < 5; i++){
  28. format_binary(B[i], bf);
  29. printf("B[%d]=(%lx)%s\n", i, B[i], bf);
  30. }
  31. static const unsigned int S[] = {1, 2, 4, 8, 16};
  32.  
  33. uint64_t x = xlo;
  34. uint64_t y = ylo;
  35. format_binary(x, bf);
  36. printf("x (%lx)%s \n", x, bf);
  37. format_binary(y, bf);
  38. printf("y (%lx)%s \n", y, bf);
  39.  
  40. x = (x | (x << S[4])) & B[4];
  41. y = (y | (y << S[4])) & B[4];
  42. format_binary(x, bf);
  43. printf("S 4: x (%lx)%s \n", x, bf);
  44. format_binary(y, bf);
  45. printf("S 4: y (%lx)%s \n", y, bf);
  46.  
  47. x = (x | (x << S[3])) & B[3];
  48. y = (y | (y << S[3])) & B[3];
  49. format_binary(x, bf);
  50. printf("S 3: x (%lx)%s \n", x, bf);
  51. format_binary(y, bf);
  52. printf("S 3: y (%lx)%s \n", y, bf);
  53.  
  54. x = (x | (x << S[2])) & B[2];
  55. y = (y | (y << S[2])) & B[2];
  56. format_binary(x, bf);
  57. printf("S 2: x (%lx)%s \n", x, bf);
  58. format_binary(y, bf);
  59. printf("S 2: y (%lx)%s \n", y, bf);
  60.  
  61. x = (x | (x << S[1])) & B[1];
  62. y = (y | (y << S[1])) & B[1];
  63. format_binary(x, bf);
  64. printf("S 1: x (%lx)%s \n", x, bf);
  65. format_binary(y, bf);
  66. printf("S 1: y (%lx)%s \n", y, bf);
  67.  
  68. x = (x | (x << S[0])) & B[0];
  69. y = (y | (y << S[0])) & B[0];
  70. format_binary(x, bf);
  71. printf("S 0: x (%lx)%s \n", x, bf);
  72. format_binary(y, bf);
  73. printf("S 0: y (%lx)%s \n", y, bf);
  74.  
  75. return x | (y << 1);
  76. }
  77.  
  78. int main(){
  79. interleave64(0xffffffff, 0);
  80. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
B[0]=(5555555555555555)101010101010101010101010101010101010101010101010101010101010101
B[1]=(3333333333333333)11001100110011001100110011001100110011001100110011001100110011
B[2]=(f0f0f0f0f0f0f0f)111100001111000011110000111100001111000011110000111100001111
B[3]=(ff00ff00ff00ff)11111111000000001111111100000000111111110000000011111111
B[4]=(ffff0000ffff)111111111111111100000000000000001111111111111111
x (ffffffff)11111111111111111111111111111111 
y (0) 
S 4: x (ffff0000ffff)111111111111111100000000000000001111111111111111 
S 4: y (0) 
S 3: x (ff00ff00ff00ff)11111111000000001111111100000000111111110000000011111111 
S 3: y (0) 
S 2: x (f0f0f0f0f0f0f0f)111100001111000011110000111100001111000011110000111100001111 
S 2: y (0) 
S 1: x (3333333333333333)11001100110011001100110011001100110011001100110011001100110011 
S 1: y (0) 
S 0: x (5555555555555555)101010101010101010101010101010101010101010101010101010101010101 
S 0: y (0)