To store the number of variables with ith bit set
int p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i , x2 , y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x2 , y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x1 - 1 , y2 ] ; else p = prefix_count [ i , x2 , y2 ] - prefix_count [ i , x1 - 1 , y2 ] - prefix_count [ i , x2 , y1 - 1 ] + prefix_count [ i , x1 - 1 , y1 - 1 ] ;