Sum vs XOR

Sort by

recency

|

54 Discussions

|

  • + 0 comments
    def sumXor(n):
        # So basically, criteria is met when all bits are flipped to 1's for an n xor x 
        #and none are flipped to zeros
        #Meaning any non-1 in n is an option for x values, fortunately x is never greater than n making
        #this a lot easier
        if n==0:return 1
        return 2**sum([True if i=='0' else False for i in format(n,'0b')])
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            long n = in.nextLong();
            long count = 0;
            while(n != 0){
                count += (n%2 == 0)?1:0;
                n/=2; 
            }
            count = (long) Math.pow(2,count);
            System.out.println(count);
        }
    }
    
  • + 0 comments
    #include <stdio.h>
    
    int main(){
        long long int n,m=1;
        scanf("%lld",&n);
        while(n>0){
            if(n%2==0)m*=2;
            n/=2;
        }
        printf("%lld\n",m);
        return 0;
    }
    
  • + 0 comments
    #include <stdio.h>
    
    int main(){
        long long int n,m=1;
        scanf("%lld",&n);
        while(n>0){
            if(n%2==0)m*=2;
            n/=2;
        }
        printf("%lld\n",m);
        return 0;
    }
    
  • + 0 comments
    int count = 0;
     for(long i =0; i<=n; i++){
         long x = n + i;
         long y = n ^ i;
         if(x == y){
           count++;
         }
     }
    return count;