fork(4) download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <functional>
  4.  
  5. //めんどくさいので8ビットで・・・Orz
  6. static const int Radix = 2;
  7. static const int s0= 0;//not need??
  8. static const int s1 = 1;
  9. static const int s2 = s1*Radix;
  10. static const int s3 = s2*Radix;
  11. static const int s4 = s3*Radix;
  12. static const int s5 = s4*Radix;
  13. static const int s6 = s5*Radix;
  14. static const int s7 = s6*Radix;
  15. static const int s8 = s7*Radix;
  16. static const int ShiftN[] = {s1, s2, s3, s4, s5, s6, s7, s8 };
  17. static const int CharBit = 8;
  18.  
  19. std::uint8_t ShiftLeft(std::uint8_t N, int P){
  20. return N * ShiftN[P];
  21. }
  22. std::uint8_t ShiftRight(std::uint8_t N, int P){
  23. std::uint8_t A = N /ShiftN[P];
  24. return A;
  25. }
  26. std::uint8_t SnipeBit(std::uint8_t N, int P){
  27. std::uint8_t A = N%ShiftN[P];
  28. std::uint8_t B = ShiftRight(A,P-1);
  29.  
  30. return B;
  31. }
  32. std::uint8_t SnipeBitWithPos(std::uint8_t N, int P){
  33. std::uint8_t A = SnipeBit(N, P);
  34.  
  35. return ShiftLeft(A,P);
  36. }
  37.  
  38. std::uint8_t TestAnd(std::uint8_t A, std::uint8_t B, int P){
  39. std::uint8_t AA = SnipeBit(A, P);
  40. std::uint8_t BB = SnipeBit(B, P);
  41. char R[] = { 0, 0, 1 };
  42.  
  43. return R[AA + BB];
  44. }
  45.  
  46. std::uint8_t TestOr(std::uint8_t A, std::uint8_t B, int P){
  47. std::uint8_t AA = SnipeBit(A, P);
  48. std::uint8_t BB = SnipeBit(B, P);
  49. char R[] = { 0, 1, 1 };
  50.  
  51. return R[AA + BB];
  52. }
  53. std::uint8_t TestXor(std::uint8_t A, std::uint8_t B, int P){
  54. std::uint8_t AA = SnipeBit(A, P);
  55. std::uint8_t BB = SnipeBit(B, P);
  56. char R[] = { 0, 1, 0 };
  57.  
  58. return R[AA + BB];
  59.  
  60. }
  61. std::uint8_t TestNot(std::uint8_t A, int P){
  62. std::uint8_t AA = SnipeBit(A, P);
  63. char R[] = { 1, 0 };
  64.  
  65. return R[AA];
  66. }
  67.  
  68. std::uint8_t ConstructNumber(std::uint8_t A, std::uint8_t B, std::function<std::uint8_t(std::uint8_t, std::uint8_t, int)> F){
  69.  
  70. std::uint8_t N = 0;
  71. int S=1;
  72. N += ShiftLeft(F(A, B, S), S-1); S++;
  73. N += ShiftLeft(F(A, B, S), S-1); S++;
  74. N += ShiftLeft(F(A, B, S), S-1); S++;
  75. N += ShiftLeft(F(A, B, S), S-1); S++;
  76. return N;
  77.  
  78. }
  79.  
  80. bool ShowBits(std::uint8_t N ,bool LF = true){
  81. for (int i = CharBit-1; i >= 0; i--)
  82. {
  83. std::cout << ((N>>i)&1);//表示関数だからいいよね?
  84. }
  85. if(LF == true) std::cout << std::endl;
  86. return true;
  87. }
  88.  
  89. int main(){
  90. /** /
  91. ShowBits(0xff);
  92. ShowBits(0x7f);
  93. ShowBits(0x7e);
  94. ShowBits(ShiftRight(0xff, 5));
  95. ShowBits(0xff>>5);
  96. ShowBits(ShiftLeft(0xff, 5));
  97. ShowBits(0xff<<5);
  98. ShowBits(SnipeBit(0x8, 4));
  99. ShowBits(SnipeBit(0xff, 3));
  100. ShowBits(0xf7);
  101. ShowBits(SnipeBit(0xf7, 4));
  102. ShowBits(SnipeBitWithPos(0xf7, 4));
  103. ShowBits(SnipeBitWithPos(0xff, 4));
  104. std::cout << "---------------------"<<std::endl;
  105. /* */
  106. ShowBits(0xc,false);std::cout << " as Value A"<<std::endl;
  107. ShowBits(0xa,false);std::cout << " as Value B"<<std::endl;
  108. ShowBits(ConstructNumber(0xc, 0xa, TestAnd),false);std::cout << " as And"<<std::endl;
  109. ShowBits(ConstructNumber(0xc, 0xa, TestOr),false);std::cout << " as OR"<<std::endl;
  110. ShowBits(ConstructNumber(0xc, 0xa, TestXor),false);std::cout << " as Xor"<<std::endl;
  111. return 0;
  112.  
  113. }
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
00001100 as Value A
00001010 as Value B
00001000 as And
00001110 as OR
00000110 as Xor