Friday 26 October 2012

Bitset built-in Function

#include<iostream>
#include<bitset>
 
using namespace std;
 
int main()
{
        for(int x; cin >> x; puts(""))
        {
                cout << bitset<32>(x) << endl;
                cout << "Number of set bits in given number: " << __builtin_popcount(x) << endl;
                cout << "Number of leading zeroes: " << __builtin_clz(x) << endl;
                cout << "Number of trailing zeroes: " << __builtin_ctz(x) << endl;
                cout << "Length of binary representation of given number: " << 32-__builtin_clz(x) << endl;
        }
        return 0;
}
 
 
 
 
input: 10
output:
00000000000000000000000001100100
Number of set bits in given number: 3
Number of leading zeroes: 25
Number of trailing zeroes: 2
Length of binary representation of given number: 7 

No comments:

Post a Comment