304 North Cardinal St.
Dorchester Center, MA 02124

Interesting XOR! CodeChef Solution

Interesting XOR! CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fast                          \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);                    \
cout.tie(NULL);
#define fileio                         \
freopen("input.txt", "r", stdin);  \
freopen("output.txt", "w", stdout);
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vpll vector<pair<ll, ll>>
#define stll set ll
#define msgll multiset<ll, greater<>>
#define msll multiset<ll>
#define mpll map<ll, ll>

void infinity()
{
ll c;
cin>>c;
ll a=log2(c);
for(ll i=0;i<a;i++)
{
c=c^(1<<i);
}
cout<<c*((1<<a)-1);
cout<<"\n";
}

int main()
{
fast
ll t;
cin >> t;
while (t--)
{
infinity();
}
return 0;
}

Interesting XOR! CodeChef Solution in C++14

#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace chrono;
// using namespace __gnu_pbds;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define int long long

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif

// typedef long long int;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

void _print(int t) {cerr << t;}
// void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
// void _print(pbds v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

/*---------------------------------------------------------------------------------------------------------------------------*/
int gcd(int a, int b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
int expo(int a, int b, int mod) {int res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(int a, int b, int*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); int x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
int mminv(int a, int b) {int arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
int mminvprime(int a, int b) {return expo(a, b - 2, b);}
bool revsort(int a, int b) {return a > b;}
void swap(int &x, int &y) {int temp = x; x = y; y = temp;}
int combination(int n, int r, int m, int *fact, int *ifact) {int val1 = fact[n]; int val2 = ifact[n - r]; int val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
int mod_add(int a, int b, int m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
int mod_mul(int a, int b, int m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
int mod_sub(int a, int b, int m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
int mod_div(int a, int b, int m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
int phin(int n) {int number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (int i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
/*--------------------------------------------------------------------------------------------------------------------------*/

void test_case() {
int n;
cin>>n;
int num1=0;
int num2=0;
bool flag=true;
for(int j=31;j>=0;j--){
if(((n>>j)&1LL)==1LL){
if(flag){
num1+=(1LL<<j);
flag=false;
}else{
num2+=(1LL<<j);
}
}else{
if(flag){
continue;
}
num1+=(1LL<<j);
num2+=(1LL<<j);
}
}
debug(mp(num1,num2))
cout<<num1*num2<<nline;
}

signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
// freopen("error.txt","w",stderr);
#endif
fastio();
auto start1 = high_resolution_clock::now();
int t=1;
cin>>t;
while(t--){
test_case();
}
auto stop1 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop1 - start1);
#ifndef ONLINE_JUDGE
cerr << "Time: " << duration . count() / 1000 << endl;
#endif
}

Interesting XOR! CodeChef Solution in PYTH 3

import math
# x=7
# y=math.log(x)//math.log(2)
# print(y)
t=int(input())
while(t):
t-=1
c=int(input())
n=int(math.log(c)/math.log(2))
n+=1
j=1
k=-1
a=0
b=0
for i in range(n):
if(c%2):
b+=j
k=j
else:
a+=j
b+=j
j*=2
c//=2
if(k!=-1):
b-=k
a+=k
print(a*b)

Interesting XOR! CodeChef Solution in C

#include <stdio.h>
#include <math.h>

int main()

{

int s;

scanf("%d", &s);

while (s--)
{

long long int c, A, B=0;

scanf("%lld", &c);

long long int size = log2(c);

A=1<<size;

for(long long int i=size-1; i>=0; i--)
{
if((c&1<<i)!=0)
{
B=B|(1<<i);

}
else if((c&1<<i)==0)
{
A|=(1<<i);

B|=(1<<i);

}
}
printf("%lld\n", A*B);
}

return 0;
}

Interesting XOR! CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(System.out));
while(tt-->0){
int fOne=0, sOne=0;
int f=0, s=0;
boolean oneIsSeen=false;
for(int i=30;i>=0;i--){
int x=1<<i;
if((x&n)!=0){
if(f!=0){
s|=x;
sOne++;
}else{
f|=x;
fOne++;
}
oneIsSeen=true;
}else if(oneIsSeen){
f|=x;
s|=x;
}
}
writer.write((f*1L*s)+"\n");
}
writer.flush();
}

private static int[] parseInt(String str) {
String[] parts=str.split("\\s+");
int[] ans=new int[parts.length];
for(int i=0;i<ans.length;i++){
ans[i]=Integer.parseInt(parts[i]);
}
return ans;
}

private static long[] parseLong(String str) {
String[] parts=str.split("\\s+");
long[] ans=new long[parts.length];
for(int i=0;i<ans.length;i++){
ans[i]=Long.parseLong(parts[i]);
}
return ans;
}
}

Interesting XOR! CodeChef Solution in PYPY 3

def decimaltobinary(n):
s=''
while n:
if n&1:
s+='1'
else:
s+='0'
n>>=1
return s[::-1]
t=int(input())
while t>0:
t-=1
c=int(input())
a=''
b=''
converted=decimaltobinary(c)
index=0
for i in converted:
if i=='1':
if a=='' or int(a,2)<=int(b,2):
a+='1'
b+='0'
else:
a+='0'
b+='1'
else:
a+='1'
b+='1'
print(int(a,2)*int(b,2))

Interesting XOR! CodeChef Solution in PYTH

import math
# x=7
# y=math.log(x)//math.log(2)
# print(y)
t=int(input())
while(t):
t-=1
c=int(input())
n=int(math.log(c)/math.log(2))
n+=1
j=1
k=-1
a=0
b=0
for i in range(n):
if(c%2):
b+=j
k=j
else:
a+=j
b+=j
j*=2
c//=2
if(k!=-1):
b-=k
a+=k
print(a*b)

Interesting XOR! CodeChef Solution in C#

using System;

public class Test
{
public static int countBits(long num) {
int count = 0;
while (num > 0) {
count++;
num >>= 1;
}

return count;
}

public static long interestingXOR(long C) {
int n = countBits(C);

long A = (1 << (n - 1)) - 1;
long B = C ^ A;

return A * B;
}

public static void Main()
{

while (T-- > 0) {
Console.WriteLine(interestingXOR(C));
}
}
}

Interesting XOR! CodeChef Solution in NODEJS

process.stdin.setEncoding('utf8');
//let INF = Infinity;
let input = '';

process.stdin.on('data', data => input += data);
process.stdin.on('end', ()=>{
input =  input.split('\n');
let index = 0, result='',
t = Number(input[index++]);
while(t--)
{
let C = N = Number(input[index++]),
binaryA = 1;
while(N != 1)
{
binaryA <<= 1;
N >>= 1;
}
//console.log('A : '+ binaryA);

binaryA--;
let B = binaryA ^ C;

//console.log('A : '+ binaryA + ' B : '+ B);
if(C <= 100000)
C = binaryA * B;
else
C = BigIntMultiplication(binaryA, B);

result += C + '\n';

}
console.log(result);
});

function BigIntMultiplication(a, b)
{
//a = a.toString().split('');
b = b.toString().split('');
let bdigits = b.length,
resultTab = [],
n = '';
for(let i = bdigits - 1; i >= 0; i--)
{
resultTab[i] = (a * b[i]) + n;
n += '0';
}

resultTab =  resultTab.reduce(BigIntSum);
return resultTab;
}

function BigIntSum(first, second)
{
first = first.split('').reverse(),
second = second.split('').reverse();
let firstDigits = first.length,
secondDigits = second.length,
sum = [],
inter = '';
if(secondDigits > firstDigits)
{
inter = first;
first = second;
second = first;
}
let retenu = 0;
for(let j = 0; j < secondDigits; j++)
{
let add = ((first[j] - 0) + (second[j] - 0) + retenu);
retenu = 1;
else
retenu = 0;
}

for(let j = secondDigits; j < firstDigits; j++)
{
let add = ((first[j] - 0) + retenu);
retenu = 1;
else
retenu = 0;
}

if(retenu)
sum.push(1);

sum = sum.reverse().join('');
return sum;

}

Interesting XOR! CodeChef Solution in GO

package main

import (
"bufio"
"bytes"
"fmt"
"os"
)

func main() {

var buf bytes.Buffer
for tc > 0 {
tc--

res := solve(n)
buf.WriteString(fmt.Sprintf("%d\n", res))
}
fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

for i := 0; i < len(s); i++ {
if s[i] == '\n' {
return s[:i]
}
}
return s
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func solve(c int) int64 {
// 2 ** d > C
// let A = 2 ** (d - 1)
// and B = C ^ A
C := int64(c)
var d uint64

for 1<<d <= C {
d++
}
var A, B int64
A = (1 << (d - 1)) - 1
B = C ^ A

return A * B
}
Interesting XOR! CodeChef Solution Review:

In our experience, we suggest you solve this Interesting XOR! CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Interesting XOR! CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Interesting XOR! CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!