__builtin 超实用位运算函数总结
①后导0(__builtin_ctz())
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout << __builtin_ctz(8) << endl;
// ans = 3 because 8 = 0b1000
return 0;
}
②前导0 (__builtin_clz())
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout << __builtin_clz(8) << endl;
// ans = 28 because 8 = 0b000000000000000000000000000001000 1前面有28个0
return 0;
}
③1的个数 (__builtin_popcount())
#include<bits/stdc++.h>
using namespace std ;
int main()
{
cout << __builtin_popcount(7) << endl ;
// ans = 3 because 7 = 0b111, 3个1.
return 0 ;
}
④快速开平方(__builtin_sqrt())
#include<bits/stdc++.h>
using namespace std ;
int main()
{
cout << __builtin_sqrt(225) << endl ;
// ans = 15 because 15 x 15 = 225,more fast!
return 0 ;
}
说明:如果是long long的话,那么加上ll即可,例如__builtin_ctzll()。
标签:__,cout,int,函数,namespace,C++,builtin,using,运算 From: https://www.cnblogs.com/gebeng/p/18045093