Join Regular Classroom : Visit ClassroomTech

IBM Question Solved | Bitwise Operations | Codewindow.in

Question:

Boolean Algebra is a branch of mathematics which deals with the binary numbers and have few operators defined for it. Some of the widely used operators on binary numbers are XOR,SHL(Shift Left),SHR(Shift Right) etc.  The below table depicts the operators that are available and to be used for this question.

 

Bitwise Operators

Mnemonic

Operator

Description

SHR

>> 

Shifts the binary digits to the right by adding 0 at at the start in the given binary word.

SHL

<< 

Shifts the binary digits to the left by adding 0 at the end in the given binary word.

XOR

^

Does an XOR operations on the individual digits of the binary word.

AND

&

Does an AND operation on the individual digits of the binary word.

OR

|

Does an OR operation on the individual digits of the binary word.

 

You are expected to do finish the function below to perform the binary operation on the input and print the result.

Note: The binary word is always an 8 bit word. Both input and output should be only of 8-bits size.

Example

Consider the input be 00011101 XOR 000000011. As evident the input operator is XOR and now when this operator that is provided in the input, or any other constraint ( as mentioned in Constraints section) then the error output should be as below:

Invalid input

 

Function Description

Complete the function binaryOperation in the editor below. The function must state what must be returned or printed.

binaryOperation has the followed parameter(s):

operation: a string which gives operation along with the operands.

 

Constraints

  • Both operands are 8 characters in length.

  • The operators should be only from the list as provided in the table above. Any other operator shall be considered as invalid

The input should be contain 2 operands and 1 operator only.

 

Sample Case 0:

Sample Input For Custom Testing
00011101  XOR  00000011
Sample Output
00011110
Explanation
After applying the operator in the individual bits of the word, we can arrive at the above result.

Sample Case 1:

Sample Input for Custom testing

AND 01010101

Sample Output

Invalid input             

Explanation

Since the input does not have any operand in the left hand side of the operator, the error is printed as above.

Input:

00011101  XOR  00000011

Output:

00011110

Solution:

JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

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

/* Name of the class has to be "Main" only if the class is public. */
class CodeWindow
{
    static int toDecimal(String str) {
        int res = 0;
        for(int i=0;i<str.length();i++) {
            int val = str.charAt(i)-48;
            res = res*2+val;
        }
        return res;
    }
    
    static void toBinary(int n) {
        String str = "";
        int temp;
        while(n>0) {
            temp = n%2;
            n /= 2;
            str += temp;
        }
        if(str.length() < 8) {
            for(int i=0;i<8-str.length();i++)
                System.out.print("0");
        }
        for(int i=0;i<str.length();i++)
            System.out.print(str.charAt(str.length()-i-1));
    }
    
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str1 = br.readLine();
		if(str1.length() < 20)
		    System.out.println("Invalid Input");
		else {
		    if(str1.length() == 20) {
		        int num1 = toDecimal(str1.substring(0, 8));
		        int num2 = toDecimal(str1.substring(12));
		        int ans = num1 | num2;
		        toBinary(ans);
		    } else if(str1.length() == 21) {
		        int num1 = toDecimal(str1.substring(0, 8));
		        int num2 = toDecimal(str1.substring(13));
		        String op = str1.substring(9,12);
		        int ans;
		        if(op.equals("AND")) {
		            ans = num1 & num2;
		            toBinary(ans);
		        } else if(op.equals("XOR")) {
		            ans = num1 ^ num2;
		            toBinary(ans);
		        } else if(op.equals("SHR")) {
		            ans = num1 >> num2;
		            toBinary(ans);
		        } else if(op.equals("SHL")) {
		            ans = num1 << num2;
		            toBinary(ans);
		        }
		    }
		}
	}
}

C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;

int toDecimal(string str) {
    int res = 0;
    for(int i=0;i<str.length();i++) {
        int val = (int)(str.at(i))-48;
        res = res*2+val;
    }
    return res;
}

void toBinary(int n) {
    string str = "";
    int temp;
    while(n>0) {
        temp = n%2;
        n /= 2;
        str += temp;
    }
    if(str.length() < 8) {
        for(int i=0;i<8-str.length();i++)
            cout << "0";
    }
    for(int i=0;i<str.length();i++)
        cout << to_string((int)str.at(str.length()-i-1));
}
    
int main()
{
	// your code goes here
	string str1;
	getline(cin, str1);
	if(str1.length() < 20)
	    cout << "Invalid Input";
    else {
	    if(str1.length() == 20) {
		    int num1 = toDecimal(str1.substr(0, 8));
		    int num2 = toDecimal(str1.substr(12));
		    int ans = num1 | num2;
		    toBinary(ans);
        } else if(str1.length() == 21) {
		    int num1 = toDecimal(str1.substr(0, 8));
		    int num2 = toDecimal(str1.substr(13));
		    string op = str1.substr(9, 3);
		    int ans;
		    if(op == "AND") {
		        ans = num1 & num2;
		        toBinary(ans);
		    } else if(op == "XOR") {
		        ans = num1 ^ num2;
		        toBinary(ans);
		    } else if(op == "SHR") {
		        ans = num1 >> num2;
		        toBinary(ans);
		    } else if(op == "SHL") {
		        ans = num1 << num2;
		        toBinary(ans);
		    }
        }
	}
}

Also Checkout

Recent Posts
Categories
Pages