การจัดลำดับบิต
Bit Manipulation หรือ การจัดลำดับบิต การจัดการบิตคือการกระทำของอัลกอริทึมการจัดการบิตหรือข้อมูลชิ้นอื่น ๆ ที่สั้นกว่าคำ งานการเขียนโปรแกรมคอมพิวเตอร์ที่ต้องมีการจัดการบิตรวมถึงการควบคุมอุปกรณ์ระดับต่ำการตรวจจับข้อผิดพลาดและอัลกอริทึมการแก้ไขการบีบอัดข้อมูลอัลกอริทึมการเข้ารหัสและการเพิ่มประสิทธิภาพ สำหรับงานอื่น ๆ ส่วนใหญ่ภาษาโปรแกรมสมัยใหม่จะอนุญาตให้โปรแกรมเมอร์ทำงานโดยตรงกับ abstractions แทนที่จะเป็นบิตที่แทน abstractions เหล่านั้น รหัสแหล่งที่จะจัดการบิตจะใช้การดำเนินการบิต: AND, OR, XOR, NOT และบิต shift การดำเนินการกับบิตถูกใช้ในการบีบอัดข้อมูล (ข้อมูลถูกบีบอัดโดยการแปลงจากการแทนหนึ่งไปยังอีกเพื่อลดพื้นที่), การเข้ารหัส (อัลกอริทึมการเข้ารหัสข้อมูลเพื่อความปลอดภัย) ในการเข้ารหัสถอดรหัสหรือบีบอัดไฟล์เราต้องแยกข้อมูลในระดับบิต การทำงานของ Bitwise ทำได้เร็วขึ้นและใกล้เคียงกับระบบและบางครั้งการเพิ่มประสิทธิภาพของโปรแกรมให้อยู่ในระดับดี
Bit Manipulation ประโยชน์[แก้]
การดำเนินการกับบิตถูกใช้ในการบีบอัดข้อมูล (ข้อมูลถูกบีบอัดโดยการแปลงจากการแทนหนึ่งไปยังอีกเพื่อลดพื้นที่), การเข้ารหัส (อัลกอริทึมการเข้ารหัสข้อมูลเพื่อความปลอดภัย) ในการเข้ารหัสถอดรหัสหรือบีบอัดไฟล์เราต้องแยกข้อมูลในระดับบิต การทำงานของ Bitwise ทำได้เร็วขึ้นและใกล้เคียงกับระบบและบางครั้งการเพิ่มประสิทธิภาพของโปรแกรมให้อยู่ในระดับดี
โดยที่ 1 ไบต์ ประกอบด้วย 8 บิตและจำนวนเต็ม หรืออักขระใด ๆ สามารถโดยใช้บิตในคอมพิวเตอร์ เรียกว่า ฟอร์มไบนารี
#-------------set bit------------
def set_bit(x,position):
mask = 1 << position # mask is number 1 move to index for position
return x | mask
# x 00000110 (6)
# position 00000101 (5) move number 1 to index 5
# mask 00100000 (32)
# (x) 00000110 (6)
# (mask) | 00100000 (32)
# ans 00100110 (38)
#------------clear bit-----------
def clear_bit(x,position):
mask = 1 << position
return x & ~mask
# x 00000110 (6)
# position 00000010 (2)
# mask 00000100 (4)
# ~mask 11111011
# 00000110 x
# & 11111011 ~mask
# ans 00000010
#------------ flip --------------
def flip_bit(x,position):
mask = 1 << position
return x ^ mask
# x 01100110
# position 00000010
# mask 00000100
# 01100110 x
# ^ 00000100 mask
#ans 01100010
#------------- bit sit ----------
def is_bit_set(x,position):
shifted = x >> position
return shifted & 1
# x 01100110
# position 00000101
# shifted 00000011
# 00000011 shifted
# & 00000001 1
#ans 00000001
#------------- modify -----------
def modify_bit(x,position,state):
mask = 1 << position
return (x & ~mask) | (-state & mask)
# x 00000110
# position 00000010
# state 00000000
# mask 00000100
# ~mask 11111011
# -state 00000000
# x &~ mask 00000010
# -state&mask 00000000
# 00000010 x & ~mask
# | 00000000 -state & mask
# ans 00000010
###### Big O (best ,worst)=> O(1)
#--------------------- if a given number is a power of 2---------
# if given number power 2 as 2 4 8 16 etc. output is True
# if given number power 2 as 3 6 9 11 etc. output is False
def isPowerOfTwo(n):
if n == 0:
return False
else:
while(n%2 == 0):
n /= 2
return n == 1
# wort case : O(logN)
# best case : O(1)
b = isPowerOfTwo(5)
print(b)
[https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/tutorial/ 1][https://www.youtube.com/watch?v=7jkIUgLC29I 1]
อ้างอิงผิดพลาด: มีป้ายระบุ <ref>
สำหรับกลุ่มชื่อ "https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/tutorial/" แต่ไม่พบป้ายระบุ <references group="https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/tutorial/"/>
ที่สอดคล้องกัน
อ้างอิงผิดพลาด: มีป้ายระบุ <ref>
สำหรับกลุ่มชื่อ "https://www.youtube.com/watch?v=7jkIUgLC29I" แต่ไม่พบป้ายระบุ <references group="https://www.youtube.com/watch?v=7jkIUgLC29I"/>
ที่สอดคล้องกัน