从python中的字节中提取LSB位

编程入门 行业动态 更新时间:2024-10-10 01:27:33
本文介绍了从python中的字节中提取LSB位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在变量 DATA中有一个字节。我想从中提取LSB位并打印。 我是python的新手,我发现很多文章都具有复杂的按位加法逻辑,而所有这些都很难理解。 我正在寻找一个简单的逻辑,例如我们对字符串的处理,例如DATA [7:1] 请帮帮我...

I have a byte in variable 'DATA'. I want to extract the LSB bit out of it and print it. I'm very new to python, I found many articles with complex bitwise addition logic and all which was very tough to understand. I'm looking for a simple logic like we do with the strings eg DATA[7:1] Please help me out...

推荐答案

您的字节是 int 吗?如果是这样,只需将 1 )与(($code>& )按位进行AND(或者,如果您想更明确一点,二进制文字 0b1 )来获取最低有效位。

Is your "byte" an int? If so, just take bitwise AND (&) with 1 (or, if you want to be more explicit, the binary literal 0b1) to get the least significant bit.

>>> x = 14 >>> x & 1 0 >>> x = 15 >>> x & 1 1

您的字节是个字节对象?如果是这样,只需对其进行索引并进行按位与。

Is your "byte" a bytes object? If so, just index into it and take bitwise AND.

>>> y = bytes([14, 15]) >>> y[0] & 1 0 >>> y[1] & 1 1

更多推荐

从python中的字节中提取LSB位

本文发布于:2023-11-28 21:34:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644020.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   python   LSB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!