Given two numbers, the task is to use alternative bits within two numbers to create result. We take first bits of second number, then second bit of the first number, third bit of second number and take the fourth bit of a first number and so on and generate a number with it.
Examples :
Input : n = 10, m = 11 Output : 11 Start from right of second number Binary representation of n = 1 0 1 0 ^ ^ Binary representation of m = 1 0 1 1 ^ ^ Output is = 1 0 1 1 Input : n = 20, m = 7 Output : 5 Start from right of second number binary representation of n = 1 0 1 0 0 ^ ^ binary representation of m = 0 0 1 1 1 ^ ^ ^ Output is = 0 0 1 0 1
Approach :-
1. Get the set even bits number of n.
2. Get the set odd bits number of m.
3. return OR of these number.
C++
// CPP Program to generate a number using alternate bits of // two numbers. #include <iostream> using namespace std; // set even bit of number n int setevenbits( int n) { int temp = n; int count = 0; // res for store 101010.. number int res = 0; // generate number form of 101010 till temp size for (temp = n; temp > 0; temp >>= 1) { // if bit is even then generate number and or with res if (count % 2 == 1) res |= (1 << count); count++; } // return set even bit number return (n & res); } // set odd bit of number m int setoddbits( int m) { int count = 0; // res for store 101010.. number int res = 0; // generate number form of 101010 till temp size for ( int temp = m; temp > 0; temp >>= 1) { // if bit is even then generate number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return set odd bit number return (m & res); } int getAlternateBits( int n, int m) { // set even bit of number n int tempn = setevenbits(n); // set odd bit of number m int tempm = setoddbits(m); // take OR with these number return (tempn | tempm); } // Driver code int main() { int n = 10; int m = 11; // n = 1 0 1 0 // ^ ^ // m = 1 0 1 1 // ^ ^ // result= 1 0 1 1 cout << getAlternateBits(n, m); return 0; } // This code is contributed by Sania Kumari Gupta (kriSania804) |
C
// C Program to generate a number using alternate bits of // two numbers. #include <stdio.h> // set even bit of number n int setevenbits( int n) { int temp = n; int count = 0; // res for store 101010.. number int res = 0; // generate number form of 101010 till temp size for (temp = n; temp > 0; temp >>= 1) { // if bit is even then generate number and or with // res if (count % 2 == 1) res |= (1 << count); count++; } // return set even bit number return (n & res); } // set odd bit of number m int setoddbits( int m) { int count = 0; // res for store 101010.. number int res = 0; // generate number form of 101010 till temp size for ( int temp = m; temp > 0; temp >>= 1) { // if bit is even then generate number and or with // res if (count % 2 == 0) res |= (1 << count); count++; } // return set odd bit number return (m & res); } int getAlternateBits( int n, int m) { // set even bit of number n int tempn = setevenbits(n); // set odd bit of number m int tempm = setoddbits(m); // take OR with these number return (tempn | tempm); } // Driver code int main() { int n = 10; int m = 11; // n = 1 0 1 0 // ^ ^ // m = 1 0 1 1 // ^ ^ // result= 1 0 1 1 printf ( "%d" , getAlternateBits(n, m)); return 0; } // This code is contributed by Sania Kumari Gupta (kriSania804) |
Java
// java Program to generate a number using // alternate bits of two numbers. import java.io.*; class GFG { // set even bit of number n static int setevenbits( int n) { int temp = n; int count = 0 ; // res for store 101010.. number int res = 0 ; // generate number form of 101010..... // till temp size for (temp = n; temp > 0 ; temp >>= 1 ) { // if bit is even then generate // number and or with res if (count % 2 == 1 ) res |= ( 1 << count); count++; } // return set even bit number return (n & res); } // set odd bit of number m static int setoddbits( int m) { int count = 0 ; // res for store 101010.. number int res = 0 ; // generate number form of 101010.... // till temp size for ( int temp = m; temp > 0 ; temp >>= 1 ) { // if bit is even then generate // number and or with res if (count % 2 == 0 ) res |= ( 1 << count); count++; } // return set odd bit number return (m & res); } static int getAlternateBits( int n, int m) { // set even bit of number n int tempn = setevenbits(n); // set odd bit of number m int tempm = setoddbits(m); // take OR with these number return (tempn | tempm); } // Driver code public static void main(String[] args) { int n = 10 ; int m = 11 ; // n = 1 0 1 0 // ^ ^ // m = 1 0 1 1 // ^ ^ // result= 1 0 1 1 System.out.println(getAlternateBits(n, m)); } } // This code is contributed by Sania Kumari Gupta // (kriSania804) |
Python3
# Python Program to generate a number using # alternate bits of two numbers. # set even bit of number n def setevenbits(n): temp = n count = 0 # res for store 101010.. number res = 0 # generate number form of 101010..... # till temp size while temp > 0 : # if bit is even then generate # number and or with res if count % 2 : res | = ( 1 << count) count + = 1 temp >> = 1 # return set even bit number return (n & res) # set odd bit of number m def setoddbits(m): temp = m count = 0 # res for store 101010.. number res = 0 # generate number form of 101010.... # till temp size while temp > 0 : # if bit is even then generate # number and or with res if not count % 2 : res | = ( 1 << count) count + = 1 temp >> = 1 # return set odd bit number return (m & res) def getAlternateBits(n, m): # set even bit of number n tempn = setevenbits(n) # set odd bit of number m tempm = setoddbits(m) # take OR with these number return (tempn | tempm) # Driver code n = 10 m = 11 # n = 1 0 1 0 # ^ ^ # m = 1 0 1 1 # ^ ^ # result= 1 0 1 1 print (getAlternateBits(n, m)) # This code is contributed by Ansu Kumari. |
C#
// C# Program to generate a number using // alternate bits of two numbers. using System; class GFG { // set even bit of number n static int setevenbits( int n) { int temp = n; int count = 0; // res for store 101010.. number int res = 0; // generate number form of 101010..... // till temp size for (temp = n; temp > 0; temp >>= 1) { // if bit is even then generate // number and or with res if (count % 2 == 1) res |= (1 << count); count++; } // return set even bit number return (n & res); } // set odd bit of number m static int setoddbits( int m) { int count = 0; // res for store 101010.. number int res = 0; // generate number form of 101010.... // till temp size for ( int temp = m; temp > 0; temp >>= 1) { // if bit is even then generate // number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return set odd bit number return (m & res); } static int getAlternateBits( int n, int m) { // set even bit of number n int tempn = setevenbits(n); // set odd bit of number m int tempm = setoddbits(m); // take OR with these number return (tempn | tempm); } // Driver code public static void Main () { int n = 10; int m = 11; // n = 1 0 1 0 // ^ ^ // m = 1 0 1 1 // ^ ^ // result= 1 0 1 1 Console.WriteLine(getAlternateBits(n, m)); } } // This code is contributed by vt_m |
PHP
<?php // PHP Program to generate a number using // alternate bits of two numbers. // set even bit of number n function setevenbits( $n ) { $temp = $n ; $count = 0; // res for store 101010.. number $res = 0; // generate number form of 101010..... // till temp size for ( $temp = $n ; $temp > 0; $temp >>= 1) { // if bit is even then generate // number and or with res if ( $count % 2 == 1) $res |= (1 << $count ); $count ++; } // return set even bit number return ( $n & $res ); } // set odd bit of number m function setoddbits( $m ) { $count = 0; // res for store 101010.. number $res = 0; // generate number form of 101010.... // till temp size for ( $temp = $m ; $temp > 0; $temp >>= 1) { // if bit is even then generate // number and or with res if ( $count % 2 == 0) $res |= (1 << $count ); $count ++; } // return set odd bit number return ( $m & $res ); } function getAlternateBits( $n , $m ) { // set even bit of number n $tempn = setevenbits( $n ); // set odd bit of number m $tempm = setoddbits( $m ); // take OR with these number return ( $tempn | $tempm ); } // Driver code $n = 10; $m = 11; // n = 1 0 1 0 // ^ ^ // m = 1 0 1 1 // ^ ^ // result= 1 0 1 1 echo getAlternateBits( $n , $m ); // This code is contributed by mits ?> |
Javascript
<script> // javascript Program to generate a number using // alternate bits of two numbers. // set even bit of number n function setevenbits(n) { var temp = n; var count = 0; // res for store 101010.. number var res = 0; // generate number form of 101010..... // till temp size for (temp = n; temp > 0; temp >>= 1) { // if bit is even then generate // number and or with res if (count % 2 == 1) res |= (1 << count); count++; } // return set even bit number return (n & res); } // set odd bit of number m function setoddbits(m) { var count = 0; // res for store 101010.. number var res = 0; // generate number form of 101010.... // till temp size for (temp = m; temp > 0; temp >>= 1) { // if bit is even then generate // number and or with res if (count % 2 == 0) res |= (1 << count); count++; } // return set odd bit number return (m & res); } function getAlternateBits(n , m) { // set even bit of number n var tempn = setevenbits(n); // set odd bit of number m var tempm = setoddbits(m); // take OR with these number return (tempn | tempm); } // Driver code var n = 10; var m = 11; // n = 1 0 1 0 // ^ ^ // m = 1 0 1 1 // ^ ^ // result= 1 0 1 1 document.write(getAlternateBits(n, m)); // This code is contributed by Princi Singh. </script> |
Output :
11
Time complexity: O(logn + logm)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!