LOGICAL BITWISE OPERATORS

LOGICAL BITWISE OPERATORS

What are bitwise operators ?

The bitwise operators are similar to the logical operators, except that they work on a smaller scale. Bitwise operators are used to change individual bits in an operand. A single byte of computer memory-when viewed as 8 bits-can signify the true/false status of 8 flags because each bit can be used as a boolean variable that can hold one of two values: true or false.

LIST OF BITWISE OPERATORS

NAME OPERATOR SYNTAX DESCRIPTION
Bitwise AND & x & y The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.
Bitwise OR | x | y The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted by |.
Bitwise NOT ~ ~x Returns one’s compliement of the number.
Bitwise XOR ^ x ^ y Returns 1 if one of the bit is 1 and other is 0 else returns false
Bitwise right shift >> x>> The left operands value is moved right by the number of bits specified by the right operand.
Bitwise left shift << x<< The left operands value is moved left by the number of bits specified by the right operand.

EXAMPLE

TERM DOCUMENT INCIDENCE MATRICES

NOTE : 1 if play contains word, 0 otherwise.

Anthony and Cleopatra Julius Caesar The Tempest Hemlet Othello Macbeth
Anthony 1 1 0 0 0 1
Brutus 1 1 0 1 0 0
Caesar 1 1 0 1 1 1
Calpurnia 0 1 0 0 0 0
Cleopatra 1 0 0 0 0 0
Mercy 1 0 1 1 1 1
worser 1 0 1 1 1 0

So in above matrices we have 0 or 1 for each term.
Example Query : Take the vectors for Brutus, Caesar and Not Calpurnia.
Brutus : 110110
Caesar : 110111
NOT Calpurnia : 101111
ANSWER : 110100 AND 110111 AND 101111 = 100100
1st and 4th bits are 1, which means first and fourth play satisfy our query.

Program to demonstrate Logical bitwise operation :

plays={"Anthony and Cleopatra":"Anthony is there, Brutus is Caeser is with Cleopatra mercy worser.",
"Julius Ceaser":"Anthony is there, Brutus is Caeser is but Calpurnia is.",
"The Tempest":"mercy worser",
"Ham let":"Caeser and Brutus are present with mercy and worser",
"Othello":"Caeser is present with mercy and worser",
"Macbeth":"Anthony is there, Caeser, mercy."}
words=["Anthony","Brutus","Caeser","Calpurnia","Cleopatra","mercy","worser"]
vector_matrix=[[0 for i in range(len(plays))] for j in range(len(words))]

text_list=list((plays.values()))

for i in range(len(words)):
for j in range(len(text_list)):
if words[i] in text_list[j]:
vector_matrix[i][j]=1
else:
vector_matrix[i][j]=0
for row in vector_matrix:
print(row)
vector_dict={}

for vector in range(len(vector_matrix)):
mystring = ""
for digit in vector_matrix[vector]:
mystring += str(digit)
vector_dict[words[vector]]=int(mystring,2)
print(vector_dict)

condition=input("Enter your condition: ")
condition1=condition
for i in words:
if i in condition1:
condition1=condition1.replace(i,str(vector_dict[i]))
print(condition1)
condition1=condition1.replace("and not"," & ~")
condition1=condition1.replace("and"," & ")
condition1=condition1.replace("or"," | ")
stri=bin(eval(condition1)).replace("0b","")
print("The plays which satisfy the condition",condition," are ")
print(stri)
for char in range(len(stri)):
if stri[char]=="1":
print(list(plays.keys())[char])

OUTPUT




Comments