hiroshima_boy's blog

By hiroshima_boy, history, 5 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

| Write comment?
»
5 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

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

Can someone explain me why i am getting 31 when i am calling __builtin_clz(0^0) as it should give 32. i am doing it using Z = l^r & then __builtin_clz(Z) it is giving 31 for l & r both zero.

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

    cause (0 xor 0) is 1.

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

      Bro how is 0^0 = 1 like bit wise 0 = ....0000 so its xor with another 0 will remain zero only, how will it become one.

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

    From GCC documentation, built-in function: int __builtin_clz (unsigned int x) returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.