Count unset bits of a number | An optimized PHP program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; Driver code
< ? php function countUnsetBits ( $ n ) { $ x = $ n ; $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; $ t = log ( $ x ^ $ n , 2 ) ; return floor ( $ t ) ; } $ n = 17 ; echo countUnsetBits ( $ n ) ; ? >