Bitwise XOR

When should you use bitwise operators? 
Saving Memory Space
Bitwise operators are good for saving memory space, but many times, space is hardly an issue. And one problem with working at the level of the individual bits is that if you decide you need more space or want to save some time. For instance, if we needed to store information about 9 cars instead of 8, then you might have to redesign large portions of your program. 

On the other hand, sometimes you can use bitwise operators to cleverly remove dependencies, such as by using ~0 to find the largest possible integer. And bit shifting to multiply by two is a fairly common operation, so it doesn't affect readability in the way that advanced use of bit manipulation can in some cases (for instance, using XOR to switch the values stored in two variables).

Encryption
There are also times when you need to use bitwise operators: if you're working with compression or some forms of encryption, or if you're working on a system that expects bit fields to be used to store Boolean attributes.

XOR(^) is a binary operation called exclusive OR which is equivalent to adding two bits and discarding the carry and works as

1^0 = 1 
0^1 = 1 
0^0 = 0
1^1 = 0 

XOR by 1 can work like a toggle switch that turns 1 to 0 or 0 to 1.

Another interesting thing to note is

x^0 = x
x^x = 0

Given two integers, L and R, find the maximal value of A xor B, where A and B satisfy the following condition:

L≤A≤B≤R

Input Format

The input contains two lines; L is present in the first line and R in the second line.

Constraints
1≤L≤R≤103

Output Format

The maximal value as mentioned in the problem statement.

Sample Input

10
15

Sample Output

7

Explanation

The input tells us that L=10 and R=15. All the pairs which comply to above condition are the following:
10⊕10=0
10⊕11=1
10⊕12=6
10⊕13=7
10⊕14=4
10⊕15=5
11⊕11=0
11⊕12=7
11⊕13=6
11⊕14=5
11⊕15=4
12⊕12=0
12⊕13=1
12⊕14=2
12⊕15=3
13⊕13=0
13⊕14=3
13⊕15=2
14⊕14=0
14⊕15=1
15⊕15=0
Here two pairs (10, 13) and (11, 12) have maximum xor value 7, and this is the answer.

#include <stdio.h>
#include <stdlib.h>

int maxXor(int l, int r) {
        int i,j,new;
        int num=0;
        for(i=l; i<=r; i++){
                for (j=i; j<=r; j++){
                        new = i ^ j;
                        if (num < new)
                                num = new;
                }
        }
        return num;

}
int main() {
        int res;
        int _l;
        printf("Enter the first number : ");
        scanf("%d", &_l);

        int _r;
        printf("Enter the second number : ");
        scanf("%d", &_r);

        res = maxXor(_l, _r);
        printf("Num is : %d", res);

        return 0;
}



Output :

Enter the first number : 10
Enter the second number : 15
Num is : 7


Now with less complexity the second solution is :

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int maxXor(int l, int r) {
    int xor = l ^ r;
    int a = 0;
    while(pow(2,a)< xor) a++;
    return (pow(2,a) - 1) ;
}

int main() {
    int res;
    int _l;
    printf("Enter the first number : ");
    scanf("%d", &_l);

    int _r;
    printf("Enter the second number : ");
    scanf("%d", &_r);

    res = maxXor(_l, _r);
    printf("Num is : %d", res);

    return 0;
}

Output :
Enter the first number : 10
Enter the second number : 15
Num is : 7

No comments:

Post a Comment