fork download
  1. #include <stdio.h>
  2.  
  3. int and(int a, int b) {
  4. int c = 0, s = 1, ta, tb;
  5. #define A1 ta = a; a /= 2; ta -= 2 * a; \
  6. tb = b; b /= 2; tb -= 2 * b; \
  7. c += ta * tb * s; s *= 2;
  8. #define A4 A1 A1 A1 A1
  9. #define A16 A4 A4 A4 A4
  10. A16 A16
  11. return c;
  12. }
  13.  
  14. int or(int a, int b) {
  15. int c = 0, s = 1, ta, tb;
  16. #define O1 ta = a; a /= 2; ta -= 2 * a; \
  17. tb = b; b /= 2; tb -= 2 * b; \
  18. c += (ta + tb - ta * tb) * s; s *= 2;
  19. #define O4 O1 O1 O1 O1
  20. #define O16 O4 O4 O4 O4
  21. O16 O16
  22. return c;
  23. }
  24.  
  25. int xor(int a, int b) {
  26. int c = 0, s = 1, ta, tb;
  27. #define X1 ta = a; a /= 2; ta -= 2 * a; \
  28. tb = b; b /= 2; tb -= 2 * b; \
  29. c += (ta + tb - 2 * ta * tb) * s; s *= 2;
  30. #define X4 X1 X1 X1 X1
  31. #define X16 X4 X4 X4 X4
  32. X16 X16
  33. return c;
  34. }
  35.  
  36. int not(int a) {
  37. return 0 - (a + 1);
  38. }
  39.  
  40. int sla(int a, int s) {
  41. if (s >= 32) return 0;
  42. if (s >= 16) a *= 0x10000, s -= 16;
  43. if (s >= 8) a *= 0x100, s -= 8;
  44. if (s >= 4) a *= 0x10, s -= 4;
  45. switch (s) {
  46. case 3: a *= 2;
  47. case 2: a *= 2;
  48. case 1: a *= 2;
  49. }
  50. return a;
  51. }
  52.  
  53. int sra(int a, int s) {
  54. if (s >= 32) return 0;
  55. if (s >= 16) a /= 0x10000, s -= 16;
  56. if (s >= 8) a /= 0x100, s -= 8;
  57. if (s >= 4) a /= 0x10, s -= 4;
  58. switch (s) {
  59. case 3: a /= 2;
  60. case 2: a /= 2;
  61. case 1: a /= 2;
  62. }
  63. return a;
  64. }
  65.  
  66. int main(void) {
  67.  
  68. int a = 0x123456;
  69. int b = 0x456789;
  70.  
  71. printf( "a = %08X\n", a );
  72. printf( "b = %08X\n", b );
  73. printf( "a and b = %08X : %08X\n", a & b, and(a, b) );
  74. printf( "a or b = %08X : %08X\n", a | b, or(a, b) );
  75. printf( "a xor b = %08X : %08X\n", a ^ b, xor(a, b) );
  76. printf( "not a = %08X : %08X\n", ~a, not(a));
  77. printf( "a << 4 = %08X : %08X\n", a << 4, sla(a, 4) );
  78. printf( "a >> 4 = %08X : %08X\n", a >> 4, sra(a, 4) );
  79. printf( "a << 19 = %08X : %08X\n", a << 19, sla(a, 19) );
  80. printf( "a >> 13 = %08X : %08X\n", a >> 13, sra(a, 13) );
  81.  
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0s 2296KB
stdin
Standard input is empty
stdout
a = 00123456
b = 00456789
a and b = 00002400 : 00002400
a or b = 005777DF : 005777DF
a xor b = 005753DF : 005753DF
not a = FFEDCBA9 : FFEDCBA9
a << 4 = 01234560 : 01234560
a >> 4 = 00012345 : 00012345
a << 19 = A2B00000 : A2B00000
a >> 13 = 00000091 : 00000091