Unit 1 - Lesson 10

10.1.1. Assignment Operators - overview

x= int(input("x: "))
y= int(input("y: "))

print("x += y: x =", x+y,"and y =", y)
print("x -= y: x =", x-y,"and y =",y)
print("x *= y: x =", x*y,"and y =",y)
print("x /= y: x =", x/y,"and y =",y)
print("x **= y: x =", x**y,"and y =",y)
print("x //= y: x =", x//y,"and y =",y)
print("x %= y: x =", x%y,"and y =",y)
print("x = y: x =", y,"and y =", y)

10.1.2. Write a program on assignment operators =,+=,-= and *=

# Assignment Operators =, +=, -=, *
x = int(input("x: "))
y = int(input("y: "))
print("x = y:", y)
print("x += y:", x+y)
print("x -= y:", x-y)
print("x *= y:", x*y)

10.1.3. Write a program on operators /=,%=,**=

# Assignment Operators  /= , %=, **=, //=
x = int(input("x: "))
y = int(input("y: "))
print('x /= y:' , x/y)
print ('x %= y:' , x%y)
print ('x **= y:' , x**y)
print ('x //= y:' , x//y)

10.2.1. Bitwise operators - an overview

(a) In (NOT) ~ Bits that are 0 become 1, and those that are 1 become 0.

10.2.2. Using Bitwise operators

#Program to illustrate bit wise operators >>, <<
x = int(input("x: "))
y = int(input("y: "))
print(x, ">>", y, "is", x>>y)
print(x, "<<", y, "is", x<<y)

10.2.3. Write a program using 'and' and 'or' Bitwise operators

#Program to illustrate the bitwise operators &, |
x = int(input("x: "))
y = int(input("y: "))
print(x, " & ", y,": ", x&y, sep='')
print(x, " | ", y, ": ", x|y, sep='')

10.2.4. Understanding Bitwise Complement Operator

(a) One's complement converts 0's into 1's and 1's into 0's.
(d) One's complement of the short int value 2 is 11111101.

10.2.5. Writing Bitwise operators ~ and ^

#Program to illustrate bitwise operators ~, ^
x = int(input("x: "))
y = int(input("y: "))
print("~ ", x, ": ", ~x, sep='')
print(x, " ^ ", y, ": ", x^y, sep='')

10.2.6. Bitwise operators - an overview

x= int(input("x: "))
y= int(input("y: "))
print(x, ">>", y, "is", x>>y)
print(x, "<<", y, "is", x<<y)
print(x, "&",  y, "is", x&y)
print(x, "|", y, "is", x|y)
print("~", x, "is", ~x)
print(x, "^", y, "is", x^y)

10.2.7. Understanding Two's Complement

x = int(input("num1: "))
y = int(input("num2: "))
print("difference:", x-y)

Post a Comment