bitshift运算符问题 [英] bitshift operator problem
问题描述
我一直在使用一个旧的8位系统并遇到了问题
<<运营商
这总是会产生0:
unsigned char i;
for(i = 0; i < 4; i ++)
{
unsigned char idx = 1<< i;
....
}
虽然按预期工作:
unsigned char i,mask = 1;
for(i = 0; i< 4; i ++)
{
unsigned char idx = mask;
....
mask = mask<< 1;
}
第一个版本是否有问题,因为这是一个8比特的
代码?
Serve Laurijssen写道:
我去过在一个旧的8位系统上工作并遇到了一个问题
<<运营商
这总是会产生0:
unsigned char i;
for(i = 0; i < 4; i ++)
{
unsigned char idx = 1<< i;
....
}
按yield 0你的意思是idx的值是
在....中总是为零吗?那应该不是这样的:idx应该在四次循环迭代中获得1,2,4,8的
。怎么
你发现idx为零(或者我没有
理解你的投诉)?
虽然这按预期工作:
unsigned char i,mask = 1;
for(i = 0; i< 4; i ++)
{
unsigned char idx = mask;
....
mask = mask<< 1;
}
您的期望是什么,以及您如何确认他们满足了?
?在这个循环中,idx和mask都应该在....期间具有值1,2,4,8。在每次迭代中,
和掩码在循环结束后应为16。
第一个版本是否有问题,考虑到这是
代码为8-bit?
首先告诉我们更多关于你的意图,以及你的
意味着发现你的代码片段正在做什么。
-
Eric Sosman
es *** **@ieee-dot-org.inva 盖子
" Eric Sosman" < es ***** @ ieee-dot-org.invalidschreef in bericht
新闻:Mc ********************* *********@comcast.com。 ..
Serve Laurijssen写道:
>我一直在做旧8 -bit系统遇到了<<<<<<<<<运营商
这总是会产生0:
unsigned char i;
for(i = 0; i< 4; i ++)
{
unsigned char idx = 1<<我;
....
}
按收益率0你的意思是idx的值是
在....中总是为零吗?那应该不是这样的:idx应该在四次循环迭代中获得1,2,4,8的
。怎么
你发现idx是零(或者我不知道b $ b了解你的投诉)?
这就是我的意思。用调试器看到了它。我猜是一个
编译器错误然后
首先告诉我们更多关于你的意图,以及你的
意味着发现代码片段正在做什么。
调试器。这些位用于设置信号,然后从寄存器中读取信号
以检查按钮是否被按下。
< blockquote> Serve Laurijssen写道,On 05/09/08 14:56:
>
" Eric Sosman" < es ***** @ ieee-dot-org.invalidschreef in bericht
新闻:Mc ********************* *********@comcast.com。 ..
> Serve Laurijssen写道:
>>我一直在工作在一个旧的8位系统上遇到了一个问题
与<<运营商
这总是会产生0:
unsigned char i;
for(i = 0; i< 4; i ++)
{
unsigned char idx = 1<< i;
....
}
按产量0你的意思是idx的值在....中始终为零。那应该不是这样的:idx应该在四次循环迭代中分别为1,2,4,8。你是如何发现idx为零(或者我没有理解你的投诉)?
这就是我的意思。用调试器看到了它。我猜一个
编译器错误然后
>首先告诉我们更多关于您的意图,以及您发现代码片段正在做什么的方法。
一个调试器。这些位用于设置信号,然后从寄存器读取一个
信号,以检查按钮是否被按下。
您是否使用了idx的值?我猜可能不是,或者可能它只是将
分配给C所认为的变量,但实际上是内存映射HW的
。检查你是否使用了''volatile''作为
声明/定义映射到HW的任何东西。
我已经实现了扫描SW之前的十六进制键盘,四处拨动
行检查是否设置了任何相关的4位,我是猜测这是你在做什么。我是这样做的,因为我是用b $ b写SW来测试键盘,通常我会用一个专用的
芯片来做它。
-
Flash Gordon
I''ve been working on an old 8-bit system and came across a problem with the
<< operator
This would always yield 0:
unsigned char i;
for (i = 0; i < 4; i++)
{
unsigned char idx = 1 << i;
....
}
while this worked as expected:
unsigned char i, mask = 1;
for (i = 0; i < 4; i++)
{
unsigned char idx = mask;
....
mask = mask << 1;
}
Is there something wrong with the first version considering that this is
code for an 8-bitter?
Serve Laurijssen wrote:I''ve been working on an old 8-bit system and came across a problem with
the << operator
This would always yield 0:
unsigned char i;
for (i = 0; i < 4; i++)
{
unsigned char idx = 1 << i;
....
}By "yield 0" do you mean that the value of idx was
always zero in "...."? That shouldn''t be so: idx should
have been 1, 2, 4, 8 on the four loop iterations. How
did you discover that idx was zero (or have I not
understood your complaint)?
while this worked as expected:
unsigned char i, mask = 1;
for (i = 0; i < 4; i++)
{
unsigned char idx = mask;
....
mask = mask << 1;
}What were your expectations, and how did you verify
that they were met? In this loop, both idx and mask should
have the values 1, 2, 4, 8 during "...." in each iteration,
and mask should be 16 after the loop ends.
Is there something wrong with the first version considering that this is
code for an 8-bitter?First tell us more about your intentions, and about your
means of discovering what your code fragments are doing.
--
Eric Sosman
es*****@ieee-dot-org.invalid
"Eric Sosman" <es*****@ieee-dot-org.invalidschreef in bericht
news:Mc******************************@comcast.com. ..Serve Laurijssen wrote:>I''ve been working on an old 8-bit system and came across a problem with
the << operator
This would always yield 0:
unsigned char i;
for (i = 0; i < 4; i++)
{
unsigned char idx = 1 << i;
....
}
By "yield 0" do you mean that the value of idx was
always zero in "...."? That shouldn''t be so: idx should
have been 1, 2, 4, 8 on the four loop iterations. How
did you discover that idx was zero (or have I not
understood your complaint)?That was what I meant yes. It was seen with a debugger. I''m guessing a
compiler bug then
First tell us more about your intentions, and about your
means of discovering what your code fragments are doing.a debugger. The bits were used to set a signal and after that read a signal
from a register to check if a button was pressed.
Serve Laurijssen wrote, On 05/09/08 14:56:>
"Eric Sosman" <es*****@ieee-dot-org.invalidschreef in bericht
news:Mc******************************@comcast.com. ..>Serve Laurijssen wrote:>>I''ve been working on an old 8-bit system and came across a problem
with the << operator
This would always yield 0:
unsigned char i;
for (i = 0; i < 4; i++)
{
unsigned char idx = 1 << i;
....
}
By "yield 0" do you mean that the value of idx was
always zero in "...."? That shouldn''t be so: idx should
have been 1, 2, 4, 8 on the four loop iterations. How
did you discover that idx was zero (or have I not
understood your complaint)?
That was what I meant yes. It was seen with a debugger. I''m guessing a
compiler bug then
> First tell us more about your intentions, and about your
means of discovering what your code fragments are doing.
a debugger. The bits were used to set a signal and after that read a
signal from a register to check if a button was pressed.Did you use the value of idx? I''m guessing possibly not, or possibly it
was only assigned to what C thought of as a variable but was actually
memory mapped HW. Check that you have used ''volatile'' for the
declaration/definition of anything which is mapped to HW.
I''ve implemented scanning a hex keypad in SW before, strobing the fours
lines checking for whether any of the relevant 4 bits are set, and I''m
guessing this is what you are doing. I was doing it because I was
writing SW to test the keypad, normally I would just use a dedicated
chip to do it.
--
Flash Gordon
这篇关于bitshift运算符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!