fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <cstdint>
  4.  
  5. template<class T>
  6. int GetBitDigit(const T& N){
  7. if (N == 0) return 0;
  8. for (std::size_t i = 0; i < std::numeric_limits<T>::digits; i++){
  9. if ((N >> i) == 0) return i;
  10. }
  11. return -1;
  12. }
  13.  
  14. std::uint64_t MakeBitOnArray(std::size_t N){
  15. std::uint64_t Num = 0;
  16. for (std::size_t i = 0; i < N; i++){
  17. Num <<= 1;
  18. Num |= 1;
  19. }
  20. return Num;
  21. }
  22.  
  23. std::uint64_t MakeHoge(std::uint64_t N){
  24. auto D = GetBitDigit(N);
  25. auto B = MakeBitOnArray(D);
  26.  
  27. auto R = N^B;
  28.  
  29. return R & B;
  30.  
  31. }
  32.  
  33. int main(){
  34.  
  35. std::uint64_t N = 0;
  36. std::uint64_t A = 0;
  37.  
  38. N = 1;
  39. A = MakeHoge(N);
  40. std::cout << N << "->" << A <<std::endl;
  41. N = 12345;
  42. A = MakeHoge(N);
  43. std::cout << N << "->" << A <<std::endl;
  44. N = 256;
  45. A = MakeHoge(N);
  46. std::cout << N << "->" << A <<std::endl;
  47. return 0;
  48. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1->0
12345->4038
256->255