-
Notifications
You must be signed in to change notification settings - Fork 1
/
Episode 10 - Operator BItwise .py
54 lines (46 loc) · 1.67 KB
/
Episode 10 - Operator BItwise .py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Episode Operator bitwise, operasi biner, binary
a = 9
b = 5
## bitwise OR (|)
c = a | b
print("===== OR =====")
print('nilai : ', a, ' , binary :', format(a, '08b')) # binary dari 9 adalah 00001001
print('nilai : ', b, ' , binary :', format(b, '08b')) # binary dari 5 adalah 00000101
print('--------------------------(|)')
print('nilai : ', c, ' , binary :', format(c, '08b')) # maka hasilnya adalah 13 dikarenakan binarynya adalah 00001101
## bitwise AND (&)
c = a & b
print("===== AND =====")
print('nilai : ', a, ' , binary :', format(a, '08b'))
print('nilai : ', a, ' , binary :', format(b, '08b'))
print('--------------------------(&)')
print('nilai :', c, ' , binary :', format(c, '08b'))
## bitwise XOR (^)
c = a ^ b
print("===== XOR =====")
print('nilai :', a, ' , binary :', format(a, '08b'))
print('nilai :', b, ' , binary :', format(b, '08b'))
print('-------------------------(^)')
print('nilai :', c, ' , binary :', format(c, '08b'))
## bitwise NOT (~)
c = a ~ b
print("===== NOT =====")
print('nilai :', a, ' , binary :', format(a, '08b'))
print('nilai :', b, ' , binary :', format(b, '08b'))
print('-------------------------(~)')
print('nilai :', c, ' , binary :', format(c, '08b'))
### shifting
# shift right (>>)
c = a >> 2
print("===== >> =====")
print('nilai :', a, ' , binary :', format(a, '08b'))
print('nilai :', b, ' , binary :', format(b, '08b'))
print('------------------(>>)')
print('nilai :', c, ' , binary :', format(c, '08b'))
# shift left (<<)
c = a << b
print("===== << =====")
print('nilai :', a, ' , binary :', format(a, '08b'))
print('nilai :', b, ' , binary :', format(b, '08b'))
print('----------------(<<)')
print('nilai :', c, ' , binary :', format(c, '08b'))