1 # Second, bitwise 2 # calculates twos complement for bit 3 # & with: two's complement operands are taken, for bit & If both bits are 1, 1 is returned, otherwise 0 4 # | or: to bits | , if there are two bits a 1, the result is 1, otherwise 0 5 # ^ XOR: ^ to bits , if the two bits are not the same as 1, otherwise 0 . 6 # << left shift: for complement moved leftward, the right to use the sign bit 0 padded. Each mobile one, is equivalent to * 2 . 7 # >> right shift: to move to the right complement, sign bit to the left to fill. Each mobile one, divided by two is equivalent to, if not divisible, corresponding to the rounded down 8 # ~ bitwise: unary operator, get complement, including the sign bit are inverted. . 9 A = 0b10001 10 B = 0b01011 . 11 # 00001 12 is 13 is C = - 2 14 D = . 5 15# - 2 1010 1110 complement 16 # 0101 0101 complement 17 # 0100 complement 18 is Print (C & D) . 19 20 is # - 2 1010 1110 complement 21 # 0101 0101 complement 22 is # | complement 1111 1001 ---- - . 1 23 is Print (C | D) 24 25 # - 2 1010 1110 complement 26 # 0101 0101 complement 27 # 1011 complement 1101 ---- - . 5 28 print(c^d) 29 30 # 2 31 # 0010 32 #00100---4 33 print(2<<1) 34 35 36 # 2 37 # 0000 0010 38 # 00 001000---8 39 print(2<<2) 40 41 print(2<<3)#---16 2*2**3=16 42 43 print(15<<3) #15*2**3 44 45 46 47 # 2 >>1 48 #0010 49 #0001----1 50 print(2>>1) 51 52 #15 >>1 53 # 0000 1111 54 # 0000 0111-----7 15//2 =7 55 print(15>>1) 56 print(-15>>1) 57 58 # 0000 1111 59 # 1111 0000----10010000--- - 16 60 print(~15)
# To sixteen machine simulation: # - 27 --------> twos complement 0B 1000 0000 0001 1011 # - 27 << 3 -----> - 27 left three twos complement 0B 1000 0000 1101 1000 --- ---> decimal - 216 # - 27 >> . 3 -----> --27 right by 3 bits twos complement 0B 1000 0000 0000 0100 ------> decimal - . 4 print(-27<<3) print(-27>>3) NUM = int (INPUT ( " Please enter a number: ' )) # N-bit left shift operation is equivalent to the 2 ^ n, then 7 = 2 ^ . 3 - 2 ^ 0 , it is understood num num subtracting the left by 3 bits Print ((NUM << . 3 ) - NUM)
multiplication: left by n bits multiplied by 2 ^ n, whereby all the numbers can change
the division: right by n bits is divided by 2 ^ n, except that no to make rounded down