Dcoder Logo

Bitwise for Dummies @ Dcoder

By Lochard | As a newbie programmer | 13 Feb 2023


Problem: This is to provide you with an introductory experience of bit-wise operations. There are basically six bit-wise operations in any programming language : - AND(&), OR(|), XOR(^), NOT(~), left-shift operator(<<) and right-shift operator(>>). You will be given 2 numbers a and b to perform AND, OR and XOR operations and 2 number p and q to perform bit-wise shifting operations(left and right) and a number x to perform the NOT(~) operation. Print the output upon performing these operations each in a newline.

 

Input: five positive integers a, b, p, q and x separated by a space.

Output: In the three lines, print the output upon performing Bitwise AND, OR, XOR operations respectively on 'a' and 'b'.

In the next two lines, print the output of left-shifting 'p' by 'q' bits and right-shifting 'p' by 'q' bits, respectively.

 

In the last line, print the output upon performing the bit-wise NOT operation on 'x'.

 

Constraints: 0 ≤ a, b, p, q, x ≤ 255

 

Sample Input: 2 4 2 1 6

 

Sample Output: 

0

6

6

4

1

-7

a, b, p, q, x = map(int, input().split())

print(a&b, a|b, a^b, p<<q, p>>q, ~x, sep='\n')

 

How do you rate this article?

1


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.