Hiểu rõ Toán tử Bitwise trong Python chỉ trong 5 phút
4 phút đọc27/08/2025
Về tác giả
I have over 5 years of experience writing technical documentation for tech products, making them accessible and user-friendly. My focus is always on providing clear and precise information.
@#@
Tôi đã có hơn 5 năm kinh nghiệm viết tài liệu kỹ thuật cho các sản phẩm công nghệ, giúp người dùng dễ dàng tiếp cận và sử dụng. Tôi luôn tập trung vào việc cung cấp thông tin chính xác và dễ hiểu.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
{"success":true,"head":"<title>Hiểu rõ Toán tử Bitwise trong Python chỉ trong 5 phút - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Toán tử Bitwise trong Python được sử dụng để thực hiện các phép toán trên các bit của số nguyên. Các số nguyên được chuyển đổi sang định dạng nhị phân, sau đó các phép toán được thực hiện từng bit một, đó là lý do chúng được gọi là "bitwise".\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"Hiểu rõ Toán tử Bitwise trong Python chỉ trong 5 phút - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Toán tử Bitwise trong Python được sử dụng để thực hiện các phép toán trên các bit của số nguyên. Các số nguyên được chuyển đổi sang định dạng nhị phân, sau đó các phép toán được thực hiện từng bit một, đó là lý do chúng được gọi là "bitwise".\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Python\" />\n<meta property=\"og:updated_time\" content=\"2026-08-03T18:07:11+07:00\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\" />\n<meta property=\"og:image:width\" content=\"1200\" />\n<meta property=\"og:image:height\" content=\"630\" />\n<meta property=\"og:image:alt\" content=\"Toán tử Bitwise trong Python\" />\n<meta property=\"og:image:type\" content=\"image/jpeg\" />\n<meta property=\"article:published_time\" content=\"2025-08-27T16:16:11+07:00\" />\n<meta property=\"article:modified_time\" content=\"2026-08-03T18:07:11+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"Hiểu rõ Toán tử Bitwise trong Python chỉ trong 5 phút - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Toán tử Bitwise trong Python được sử dụng để thực hiện các phép toán trên các bit của số nguyên. Các số nguyên được chuyển đổi sang định dạng nhị phân, sau đó các phép toán được thực hiện từng bit một, đó là lý do chúng được gọi là "bitwise".\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Chris Pham\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"4 minutes\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\",\"url\":\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\",\"width\":\"1200\",\"height\":\"630\",\"caption\":\"To\\u00e1n t\\u1eed Bitwise trong Python\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/category/python/\",\"name\":\"Python\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/\",\"name\":\"Hi\\u1ec3u r\\u00f5 To\\u00e1n t\\u1eed Bitwise trong Python ch\\u1ec9 trong 5 ph\\u00fat\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/\",\"name\":\"Hi\\u1ec3u r\\u00f5 To\\u00e1n t\\u1eed Bitwise trong Python ch\\u1ec9 trong 5 ph\\u00fat - CyStack Tutorial\",\"datePublished\":\"2025-08-27T16:16:11+07:00\",\"dateModified\":\"2026-08-03T18:07:11+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"name\":\"Chris Pham\",\"url\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/7a3584c941fa360ac061b2a8f15684ced7b92daa1f09481be135a684d989e9b9?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/7a3584c941fa360ac061b2a8f15684ced7b92daa1f09481be135a684d989e9b9?s=96&d=mm&r=g\",\"caption\":\"Chris Pham\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"Hi\\u1ec3u r\\u00f5 To\\u00e1n t\\u1eed Bitwise trong Python ch\\u1ec9 trong 5 ph\\u00fat - CyStack Tutorial\",\"keywords\":\"To\\u00e1n t\\u1eed Bitwise trong Python,Bitwise,To\\u00e1n t\\u1eed Bitwise\",\"datePublished\":\"2025-08-27T16:16:11+07:00\",\"dateModified\":\"2026-08-03T18:07:11+07:00\",\"articleSection\":\"Python\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"name\":\"Chris Pham\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"To\\u00e1n t\\u1eed Bitwise trong Python \\u0111\\u01b0\\u1ee3c s\\u1eed d\\u1ee5ng \\u0111\\u1ec3 th\\u1ef1c hi\\u1ec7n c\\u00e1c ph\\u00e9p to\\u00e1n tr\\u00ean c\\u00e1c bit c\\u1ee7a s\\u1ed1 nguy\\u00ean. C\\u00e1c s\\u1ed1 nguy\\u00ean \\u0111\\u01b0\\u1ee3c chuy\\u1ec3n \\u0111\\u1ed5i sang \\u0111\\u1ecbnh d\\u1ea1ng nh\\u1ecb ph\\u00e2n, sau \\u0111\\u00f3 c\\u00e1c ph\\u00e9p to\\u00e1n \\u0111\\u01b0\\u1ee3c th\\u1ef1c hi\\u1ec7n t\\u1eebng bit m\\u1ed9t, \\u0111\\u00f3 l\\u00e0 l\\u00fd do ch\\u00fang \\u0111\\u01b0\\u1ee3c g\\u1ecdi l\\u00e0 "bitwise".\",\"name\":\"Hi\\u1ec3u r\\u00f5 To\\u00e1n t\\u1eed Bitwise trong Python ch\\u1ec9 trong 5 ph\\u00fat - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/27161438/toan-tu-bitwise-trong-python.jpg\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/vi/2025/08/27/toan-tu-bitwise-trong-python/#webpage\"}}]}</script>\n"}
Toán tử Bitwise trong Python được sử dụng để thực hiện các phép toán trên các bit của số nguyên. Các số nguyên được chuyển đổi sang định dạng nhị phân, sau đó các phép toán được thực hiện từng bit một, đó là lý do chúng được gọi là “bitwise”.

Các toán tử này chỉ hoạt động trên các số nguyên và kết quả cuối cùng được trả về dưới định dạng số thập phân. Python bitwise operators còn được gọi là binary operators (toán tử nhị phân).
Các toán tử Bitwise trong Python
Có 6 toán tử bitwise trong Python. Bảng dưới đây cung cấp chi tiết ngắn gọn về chúng:
Toán tử Bitwise AND (&)
Toán tử bitwise AND trong Python trả về 1 nếu cả hai bit ở cùng vị trí đều là 1, ngược lại thì trả về 0.
>>> 10&7
2
>>>

Toán tử Bitwise OR (|)
Toán tử bitwise OR trong Python trả về 1 nếu một trong hai bit ở cùng vị trí là 1. Nếu cả hai bit đều là 0, nó sẽ trả về 0.
>>> 10|7
15
>>>

Toán tử Bitwise XOR (^)
Toán tử bitwise XOR trong Python trả về 1 nếu một trong hai bit là 0 và bit còn lại là 1. Nếu cả hai bit đều là 0 hoặc 1 (tức là chúng giống nhau), nó sẽ trả về 0.
>>> 10^7
13
>>>

Toán tử Bitwise Ones’ Complement (~)
Toán tử Ones’ complement của một số ‘A’ trong Python sẽ tương đương với -(A+1).
>>> ~10
-11
>>> ~-10
9
>>>

Toán tử Bitwise Left Shift (<<)
Toán tử bitwise left shift trong Python dịch chuyển các bit của toán hạng bên trái sang phía bên trái một số lần bằng với giá trị của toán hạng bên phải. Nói một cách đơn giản, các số 0 sẽ được thêm vào cuối số nhị phân
>>> 10 << 2
40
>>>

Toán tử Bitwise Right Shift (>>)
Toán tử bitwise right shift trong Python hoàn toàn ngược lại với toán tử left shift. Các bit của toán hạng bên trái sẽ được dịch chuyển sang phía bên phải một số lần bằng với giá trị của toán hạng bên phải. Nói một cách đơn giản, các bit ở phía bên phải sẽ bị loại bỏ.
>>> 10 >> 2
2
>>>

Nạp chồng toán tử Bitwise trong Python
Python hỗ trợ nạp chồng toán tử. Có nhiều phương thức mà chúng ta có thể triển khai để hỗ trợ các toán tử bitwise cho các custom objects của mình.
Ví dụ về nạp chồng toán tử bitwise cho một custom object:
class Data:
id = 0
def __init__(self, i):
self.id = i
def __and__(self, other):
print('Bitwise AND operator overloaded')
if isinstance(other, Data):
return Data(self.id & other.id)
else:
raise ValueError('Argument must be object of Data')
def __or__(self, other):
print('Bitwise OR operator overloaded')
if isinstance(other, Data):
return Data(self.id | other.id)
else:
raise ValueError('Argument must be object of Data')
def __xor__(self, other):
print('Bitwise XOR operator overloaded')
if isinstance(other, Data):
return Data(self.id ^ other.id)
else:
raise ValueError('Argument must be object of Data')
def __lshift__(self, other):
print('Bitwise Left Shift operator overloaded')
if isinstance(other, int):
return Data(self.id << other)
else:
raise ValueError('Argument must be integer')
def __rshift__(self, other):
print('Bitwise Right Shift operator overloaded')
if isinstance(other, int):
return Data(self.id >> other)
else:
raise ValueError('Argument must be integer')
def __invert__(self):
print('Bitwise Ones Complement operator overloaded')
return Data(~self.id)
def __str__(self):
return f'Data[{self.id}]'
d1 = Data(10)
d2 = Data(7)
print(f'd1&d2 = {d1&d2}')
print(f'd1|d2 = {d1|d2}')
print(f'd1^d2 = {d1^d2}')
print(f'd1<<2 = {d1<<2}')
print(f'd1>>2 = {d1>>2}')
print(f'~d1 = {~d1}')
Kết quả
Bitwise AND operator overloaded
d1&d2 = Data[2]
Bitwise OR operator overloaded
d1|d2 = Data[15]
Bitwise XOR operator overloaded
d1^d2 = Data[13]
Bitwise Left Shift operator overloaded
d1<<2 = Data[40]
Bitwise Right Shift operator overloaded
d1>>2 = Data[2]
Bitwise Ones Complement operator overloaded
~d1 = Data[-11]
Nếu bạn chưa quen với định dạng chuỗi mới, vui lòng đọc thêm về f-strings trong Python.
Tổng kết
Toán tử bitwise trong Python chủ yếu được sử dụng trong các phép tính toán học. Chúng ta cũng có thể triển khai các phương thức cụ thể để hỗ trợ các toán tử bitwise cho các lớp tùy chỉnh của mình.