When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

hiroshima_boy's blog

By hiroshima_boy, history, 4 years ago, In English

1.) __builtin_popcount(x): Counts the number of one’s(set bits) in an integer(long/long long).

Ex- int x=5;
    cout<<__builtin_popcount(x)<<endl;   //returns 2.

If x is of long type,we can use __builtin_popcountl(x) If x is of long long type,we can use __builtin_popcountll(x)

2.) __builtin_parity(x): Checks the Parity of a number.Returns true(1) if the number has odd parity(odd number of set bits) else it returns false(0) for even parity(even number of set bits).

Ex- int x=5;
    cout<<__builtin_parity(x)<<endl;   //returns 0.

If x is of long type,we can use __builtin_parityl(x) If x is of long long type,we can use __builtin_parityll(x)

3.) __builtin_clz(x): Counts the leading number of zeros of the integer(long/long long).

If x is of long type,we can use __builtin_clzl(x) If x is of long long type,we can use __builtin_clzll(x)

Ex- int x=16;       // 00000000 00000000 00000000 00010000 (32 bits)
      cout<<__builtin_clz(x)<<endl;   //returns 27.

  Ex- long x=16;       // 00000000 00000000 00000000 00010000 (32 bits)
      cout<<__builtin_clzl(x)<<endl;   //returns 27.

  Ex- long long x=16;       // 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00010000 (64 bits)
      cout<<__builtin_clzll(x)<<endl;   //returns 59.

4.) __builtin_ctz(x): Counts the trailing number of zeros of the integer(long/long long).

If x is of long type,we can use __builtin_ctzl(x) If x is of long long type,we can use __builtin_ctzll(x)

Ex- int x=16;       // 00000000 00000000 00000000 00010000 (32 bits)
    cout<<__builtin_ctz(x)<<endl;   //returns 4.

In case of both __builtin_ctzl(x) and __builtin_ctzll(x),the answer is same.

  • Vote: I like it
  • +7
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

There's also __builtin_ffs(x) (Find First Set) which returns (the index of the least significant bits of x) + 1.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

So I need to find the leading zeroes of say 1e15 , and I used builtinclzll(x) , where x is initialized as long long data type , but at the end , the function returns the leading zeroes from the 32 bit representation. I also cross-verified it using builtinclzll(1e15) , but the result is still the same. help pls

»
15 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I have faced the word 'parity' many times when I have worked with bits. Can anyone explain what parity really is?

  • »
    »
    15 months ago, # ^ |
    Rev. 2   Vote: I like it -8 Vote: I do not like it
    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Nope, it's the parity of the number of set bits (bits that have value 1). So let's take 5 for example, 101 in binary system has even parity because it has 2 set bits which is an even number of set bits. On the other hand, 7, 111 in binary has 3 set bits which makes it have an odd parity.

      • »
        »
        »
        »
        5 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        you are also right. It can mean both things,check this link(under parity section). Wikipedia link

        • »
          »
          »
          »
          »
          5 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Mathematical parity refers to evenness or oddnessn of a number.