fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. #define demo(f,x) cout << #f << "(" << x << ") = " << f(x) << endl
  5.  
  6. template<typename T> int ones_count_32(T x) { return __builtin_popcount(x); }
  7. template<typename T> int leading_zeros_count_32(T x) { return __builtin_clz(x); }
  8. template<typename T> int trailing_zeros_count_32(T x) { return __builtin_ctz(x); }
  9.  
  10. template<typename T> int ones_count_64(T x) { return __builtin_popcountll(x); }
  11. template<typename T> int leading_zeros_count_64(T x) { return __builtin_clzll(x); }
  12. template<typename T> int trailing_zeros_count_64(T x) { return __builtin_ctzll(x); }
  13.  
  14. void demo_32(int x) {
  15. demo(ones_count_32,x), demo(leading_zeros_count_32,x), demo(trailing_zeros_count_32,x); }
  16.  
  17. void demo_64(ll y) {
  18. demo(ones_count_64,y), demo(leading_zeros_count_64,y), demo(trailing_zeros_count_64,y); }
  19.  
  20. int main() {
  21. int x; ll y; cin >> x >> y, demo_32(x), demo_64(y); }
  22.  
Success #stdin #stdout 0s 4552KB
stdin
22222 22222222222222222
stdout
ones_count_32(22222) = 9
leading_zeros_count_32(22222) = 17
trailing_zeros_count_32(22222) = 1
ones_count_64(22222222222222222) = 33
leading_zeros_count_64(22222222222222222) = 9
trailing_zeros_count_64(22222222222222222) = 1