PHP Professional এবং Zend Certified PHP Engineer (ZCPE ) Course এ সীমিত সংখ্যক আসন বাকি আছে। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

Sum vs XOR, Bit manipulation problem

Given an integer, n, find each x such that:

  • 0<=x <=n
  • n+x = n^ x

where ^  denotes the bitwise XOR operator. Then print an integer denoting the total number of x‘s satisfying the criteria above.

Input Format

A single integer, n.


  • 0<=n <=10**15


  • 0<=n <=100 for 60% of the maximum score.

Output Format

Print the total number of integer x ‘s satisfying both of the conditions specified above.

Sample Input 0


Sample Output 0


Explanation 0

For n=5 , the x  values 0  and 2 satisfy the conditions:

  • 5 + 0 =  5 ^ 0 = 5
  • 5 + 2 = 5 ^ 2 = 5

Thus, we print 2 as our answer.

Sample Input 1


Sample Output 1



Solution in PHP

problem link Click Here

If any problem, feel free to comment or contact.


This solution is totally a mathematical trick.

just count total numbers of ZERO present in binary number of given n, and answer will be the 2 to the power of (total num of zero).


Here one line solution
$handle = fopen ("php://stdin","r");

echo ($n>0)?pow(2,substr_count(decbin($n),'0')):1;
 This also same logic

$handle = fopen ("php://stdin","r");

$count += fmod($n,2) ? 0 : 1;
$n /=2;
echo pow(2,$count);
$handle = fopen ("php://stdin","r");

$count += bcmod($n,2) ? 0 : 1;
$n /=2;
echo bcpow(2,$count);

I am PHP problem solver at Hackerrank. I am preparing myself for PHP zend certification exam with Masud Alam sir.
I have completed few websites using LARAVEL, also have experience on WORDPRESS.

Leave a Reply

Your email address will not be published. Required fields are marked *

No announcement available or all announcement expired.