简体   繁体   English

python按位运算

[英]python bitwise operation

hi i am new in python just started learning with python i got a task in which i need to store "1" byte of integer into different bits just like RGB the value are store in that can any one would write a small program for me and explain that ,please i need a help 嗨,我是python的新手,刚开始使用python学习,我得到了一个任务,我需要将整数的“ 1”字节存储到不同的位中,就像RGB一样,该值存储在其中,任何人都可以为我编写一个小程序,解释一下,请我帮忙

Thankyou 谢谢

I'll assume this question is legitimate and appropriate for the forum.. 我认为这个问题是合法的,并且适合该论坛。

# To Encode:
r = 1
g = 2
b = 3

rgb = r << 16 | g << 8 | b

#To extract:
r = (rgb >> 16) & 0xFF
g = (rgb >> 8) & 0xFF
b = rgb & 0xFF

要将数字转换为二进制数list(bin(number))[2:]list(bin(number))[2:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM