如何从字节到位变量分配一个位? [英] How do you assign a bit from a byte to a bit variable?

查看:74
本文介绍了如何从字节到位变量分配一个位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个1位宽的变量。我还有一个变量是一个

字节。我想将这些位从字节中移出到位变量

(一次一个),但我不知道该怎么做或者它是否是偶数

可能。这是我的代码的样子:


//数据是一个大小为1个字节的变量,而bitVariable是一个大小为
的1位。我是一个字节。


for(i = 0; i< 8; i ++)

{

bitVariable =(数据<< i)& 0x80;

}


我不相信这会有效,因为没有迹象表明

真的被分配了to bitVariable。我的编译器没有抱怨和

我发现这很奇怪。本质上,我需要将数据中的位移位。在我的微控制器上输出一个输出端口,并希望尽可能快地完成它。

I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;
}

I don''t believe this will work because there is no indication what is
really being assigned to bitVariable. My compiler doesn''t complain and
I find that strange. Essentially, I need to shift the bits in "data" to
an output port on my microcontroller and want to do it as quickly as
possible.

推荐答案

jo*********@yahoo.com 写道:
我有一个1位宽的变量。我还有一个变量是一个
字节。我想将这些位从字节中移位到位变量
(一次一个),但我不知道如何做到这一点,或者它是否可能。以下是我的代码:/ //
//数据大小为1个字节,而bitVariable大小为1位。我是一个字节。

for(i = 0; i< 8; i ++)
{/ / bitVariable =(data<< i)& 0x80;
I have a variable that is 1 bit wide. I also have a variable that is a
byte. I want to shift the bits out of the byte into the bit variable
(one at a time) but am not sure how to do this or whether it is even
possible. Here is what my code looks like:

// data is a variable 1 byte in size while bitVariable is 1 bit in
size. i is a byte.

for (i = 0; i < 8; i++)
{
bitVariable = (data << i) & 0x80;




你想要数据在MS位吗?


应该是


bitVariable =(data>> i)& 0x01;


-

Ian Collins。



Do you want the data to be in the MS bit?

Should it be

bitVariable = (data >> i) & 0x01;

--
Ian Collins.


将数据移出' 'data''变量,必须将最高位
有效位的位分配给变量

''bitVariable''。

Ian Collins写道:
When shifting the data out of the ''data'' variable, the bit in the most
significant bit position must be assigned to the variable
''bitVariable''.
Ian Collins wrote:
你想要数据在MS位吗?

应该是

bitVariable =(data>> ; i)& 0x01;

- Ian Collins。
Do you want the data to be in the MS bit?

Should it be

bitVariable = (data >> i) & 0x01;

--
Ian Collins.






jo ********* @ yahoo.com 写道:
Ian Collins写道:
Ian Collins wrote:
你想要数据在MS位吗?

应该是

bitVariable =(data>> i) &安培; 0x01;
Do you want the data to be in the MS bit?

Should it be

bitVariable = (data >> i) & 0x01;


当从数据变量中移出数据时,必须将最高位位置的位分配给变量
''bitVariable' '。


When shifting the data out of the ''data'' variable, the bit in the most
significant bit position must be assigned to the variable
''bitVariable''.



请不要发帖。


所以bitVariable是一个硬件注册?


-

Ian Collins。


Please don''t top post.

So bitVariable is a hardware register?

--
Ian Collins.


这篇关于如何从字节到位变量分配一个位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆