# Count trailing zero bits using lookup table

Given an integer, count the number of trailing zeroes. For example, for n = 12, its binary representation is 1100 and number of trailing zero bits is 2.

**Examples :**

Input : 8 Output : 3 Binary of 8 is 1000, so there are theree trailing zero bits. Input : 18 Output : 1 Binary of 18 is 10010, so there is one trailing zero bit.

A **simple solution **is to traverse bits from LSB (Least Significant Bit) and increment count while bit is 0.

## C++

`// Simple C++ code for counting trailing zeros ` `// in binary representation of a number ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countTrailingZero(` `int` `x) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `((x & 1) == 0) ` ` ` `{ ` ` ` `x = x >> 1; ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `cout << countTrailingZero(11) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Simple Java code for counting ` `// trailing zeros in binary ` `// representation of a number ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `countTrailingZero(` `int` `x) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `while` `((x & ` `1` `) == ` `0` `) ` ` ` `{ ` ` ` `x = x >> ` `1` `; ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `System.out.println(countTrailingZero(` `11` `)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 code for counting trailing zeros ` `# in binary representation of a number ` `def` `countTrailingZero(x): ` ` ` `count ` `=` `0` ` ` `while` `((x & ` `1` `) ` `=` `=` `0` `): ` ` ` `x ` `=` `x >> ` `1` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `print` `(countTrailingZero(` `11` `)) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// Simple C# code for counting ` `// trailing zeros in binary ` `// representation of a number ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `countTrailingZero(` `int` `x) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `while` `((x & 1) == 0) ` ` ` `{ ` ` ` `x = x >> 1; ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `Console.WriteLine(countTrailingZero(11)); ` ` ` `} ` `} ` ` ` `// This code is contributed by aj_36 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Simple PHP code for counting trailing zeros ` `// in binary representation of a number ` ` ` `function` `countTrailingZero(` `$x` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `((` `$x` `& 1) == 0) ` ` ` `{ ` ` ` `$x` `= ` `$x` `>> 1; ` ` ` `$count` `++; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `echo` `countTrailingZero(11),` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

0

**Time Complexity : **O(Log n)

The **lookup table solution** is based on following concepts :

- The solution assumes that negative numbers are stored in 2’s complement form which is true for most of the devices. If numbers are represented in 2’s complement form, then (x & -x) [Bitwise and of x and minus x] produces a number with only last set bit.
- Once we get a number with only one bit set, we can find its position using lookup table. It makes use of the fact that the first 32 bit position values are relatively prime with 37, so performing a modulus division with 37 gives a unique number from 0 to 36 for each. These numbers may then be mapped to the number of zeros using a small lookup table.

## C++

`// C++ code for counting trailing zeros ` `// in binary representation of a number ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countTrailingZero(` `int` `x) ` `{ ` ` ` `// Map a bit value mod 37 to its position ` ` ` `static` `const` `int` `lookup[] = {32, 0, 1, ` ` ` `26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, ` ` ` `0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29, ` ` ` `10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, ` ` ` `18}; ` ` ` ` ` `// Only difference between (x and -x) is ` ` ` `// the value of signed magnitude(leftmostbit) ` ` ` `// negative numbers signed bit is 1 ` ` ` `return` `lookup[(-x & x) % 37]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `cout << countTrailingZero(48) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code for counting ` `// trailing zeros in binary ` `// representation of a number ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `countTrailingZero(` `int` `x) ` `{ ` ` ` ` ` `// Map a bit value mod ` ` ` `// 37 to its position ` ` ` `int` `lookup[] = {` `32` `, ` `0` `, ` `1` `, ` `26` `, ` `2` `, ` `23` `, ` ` ` `27` `, ` `0` `, ` `3` `, ` `16` `, ` `24` `, ` `30` `, ` ` ` `28` `, ` `11` `, ` `0` `, ` `13` `, ` `4` `, ` `7` `, ` ` ` `17` `, ` `0` `, ` `25` `, ` `22` `, ` `31` `, ` `15` `, ` ` ` `29` `, ` `10` `, ` `12` `, ` `6` `, ` `0` `, ` `21` `, ` ` ` `14` `, ` `9` `, ` `5` `, ` `20` `, ` `8` `, ` `19` `, ` `18` `}; ` ` ` ` ` `// Only difference between ` ` ` `// (x and -x) is the value ` ` ` `// of signed magnitude ` ` ` `// (leftmostbit) negative ` ` ` `// numbers signed bit is 1 ` ` ` `return` `lookup[(-x & x) % ` `37` `]; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `System.out.println(countTrailingZero(` `48` `)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for counting trailing zeros ` `# in binary representation of a number ` ` ` `def` `countTrailingZero(x): ` ` ` ` ` `# Map a bit value mod 37 to its position ` ` ` `lookup ` `=` `[` `32` `, ` `0` `, ` `1` `, ` `26` `, ` `2` `, ` `23` `, ` `27` `, ` `0` `, ` ` ` `3` `, ` `16` `, ` `24` `, ` `30` `, ` `28` `, ` `11` `, ` `0` `, ` `13` `, ` ` ` `4` `, ` `7` `, ` `17` `, ` `0` `, ` `25` `, ` `22` `, ` `31` `, ` `15` `, ` ` ` `29` `, ` `10` `, ` `12` `, ` `6` `, ` `0` `, ` `21` `, ` `14` `, ` `9` `, ` ` ` `5` `, ` `20` `, ` `8` `, ` `19` `, ` `18` `] ` ` ` ` ` `# Only difference between (x and -x) is ` ` ` `# the value of signed magnitude(leftmostbit) ` ` ` `# negative numbers signed bit is 1 ` ` ` `return` `lookup[(` `-` `x & x) ` `%` `37` `] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `print` `(countTrailingZero(` `48` `)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# code for counting ` `// trailing zeros in binary ` `// representation of a number ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `countTrailingZero(` `int` `x) ` `{ ` ` ` ` ` `// Map a bit value mod ` ` ` `// 37 to its position ` ` ` `int` `[]lookup = {32, 0, 1, 26, 2, 23, ` ` ` `27, 0, 3, 16, 24, 30, ` ` ` `28, 11, 0, 13, 4, 7, ` ` ` `17, 0, 25, 22, 31, 15, ` ` ` `29, 10, 12, 6, 0, 21, ` ` ` `14, 9, 5, 20, 8, 19, 18}; ` ` ` ` ` `// Only difference between ` ` ` `// (x and -x) is the value ` ` ` `// of signed magnitude ` ` ` `// (leftmostbit) negative ` ` ` `// numbers signed bit is 1 ` ` ` `return` `lookup[(-x & x) % 37]; ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `Console.WriteLine(countTrailingZero(48)); ` `} ` `} ` ` ` `// This code is contributed ` `// by m_kit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code for counting ` `// trailing zeros in binary ` `// representation of a number ` ` ` `function` `countTrailingZero(` `$x` `) ` `{ ` ` ` `// Map a bit value mod ` ` ` `// 37 to its position ` ` ` `$lookup` `= ` `array` `(32, 0, 1, 26, 2, 23, ` ` ` `27, 0, 3, 16, 24, 30, ` ` ` `28, 11, 0, 13, 4, 7, ` ` ` `17, 0, 25, 22, 31, 15, ` ` ` `29, 10, 12, 6, 0, 21, ` ` ` `14, 9, 5, 20, 8, 19, 18); ` ` ` ` ` `// Only difference between ` ` ` `// (x and -x) is the value ` ` ` `// of signed magnitude ` ` ` `// (leftmostbit) negative ` ` ` `// numbers signed bit is 1 ` ` ` `return` `$lookup` `[(-` `$x` `& ` ` ` `$x` `) % 37]; ` `} ` ` ` `// Driver Code ` `echo` `countTrailingZero(48), ` `"\n"` `; ` ` ` `// This code is contributed ` `// by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

**Time Complexity : **O(1)

**Source : **

https://graphics.stanford.edu/~seander/bithacks.html

This article is contributed by **Sumit Sudhakar(Sam)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Write an Efficient C Program to Reverse Bits of a Number
- Count set bits in an integer
- Count number of bits to be flipped to convert A to B
- Rotate bits of a number
- Next higher number with same number of set bits
- Program to count number of set bits in an (big) array
- Count total set bits in all numbers from 1 to n
- Swap bits in a given number
- Swap all odd and even bits
- Toggle all the bits of a number except k-th bit.
- Count inversions in an array | Set 3 (Using BIT)
- Count number of subsets having a particular XOR value
- Count strings with consecutive 1's
- Copy set bits in a range
- Print first n numbers with exactly two set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Count all pairs with given XOR
- Check if a number has bits in alternate pattern | Set 1
- Count smaller numbers whose XOR with n produces greater value
- Count minimum bits to flip such that XOR of A and B equal to C