Rabu, 13 Agustus 2025

Operator bitwise pemrograman python di VSCode

1.       Operasi biwise atau disebut sebagai operasi biner

Operator bitwise terdiri dari or, and, xor, not

 

2.       Source code

 

# operator bitwise atau operasi biner

 

a = 9

b = 3

 

# bitwise OR menggunakan tanda |

c = a | b

print ("Nilai :",a,", binary :",format(a,"08b")) # memformat ke bentuk biner

print ("Nilai :",b,", binary :",format(b,"08b")) # memformat ke bentuk biner

print ("Nilai :",c,", binary :",format(c,"08b")) # memformat ke bentuk biner

print ()

 

# bitwise AND menggunakan tanda &

c = a & b

print ("Nilai :",a,", binary :",format(a,"08b")) # memformat ke bentuk biner

print ("Nilai :",b,", binary :",format(b,"08b")) # memformat ke bentuk biner

print ("Nilai :",c,", binary :",format(c,"08b")) # memformat ke bentuk biner

print ()

 

# bitwise XOR menggunakan tanda ^

c = a ^ b

print ("Nilai :",a,", binary :",format(a,"08b")) # memformat ke bentuk biner

print ("Nilai :",b,", binary :",format(b,"08b")) # memformat ke bentuk biner

print ("Nilai :",c,", binary :",format(c,"08b")) # memformat ke bentuk biner

print ()

 

# bitwise NOT menggunakan tanda ~

c = ~a

print ("Nilai :",a,", binary :",format(a,"08b")) # memformat ke bentuk biner

print ("Nilai :",c,", binary :",format(c,"08b")) # memformat ke bentuk biner

print ()

 

d = 0b0000001001

e = 0b1111111111

print ("Nilai :",e^d,",binery :", format(e^d,"08b"))

print ()

 

# shifting

# shifting right >> untuk menggeser ke kanan

c = a >> 2

print ("Nilai :",a,", binary :",format(a,"08b")) # memformat ke bentuk biner

print ("Nilai :",c,", binary :",format(c,"08b")) # memformat ke bentuk biner

print ()

 

# shifting left << untuk menggeser ke kiri

c = a << 2

print ("Nilai :",a,", binary :",format(a,"08b")) # memformat ke bentuk biner

print ("Nilai :",c,", binary :",format(c,"08b")) # memformat ke bentuk biner

print ()

 


Tidak ada komentar:

Posting Komentar

While loop pemrograman python di VSCode

1.        While bernilai boolean 2.        Source code   # while loop   # while kondisi : #   aksi #   aksi # akhir dari p...