本文介绍了bitshift运算符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在使用一个旧的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比特的代码? 解决方案 按yield 0你的意思是idx的值是 在....中总是为零吗?那应该不是这样的:idx应该在四次循环迭代中获得1,2,4,8的。怎么 你发现idx为零(或者我没有 理解你的投诉)? 您的期望是什么,以及您如何确认他们满足了??在这个循环中,idx和mask都应该在....期间具有值1,2,4,8。在每次迭代中, 和掩码在循环结束后应为16。 首先告诉我们更多关于你的意图,以及你的 意味着发现你的代码片段正在做什么。 - Eric Sosman es *** **@ieee-dot-org.inva 盖子 按收益率0你的意思是idx的值是 在....中总是为零吗?那应该不是这样的:idx应该在四次循环迭代中获得1,2,4,8的。怎么 你发现idx是零(或者我不知道b $ b了解你的投诉)? 这就是我的意思。用调试器看到了它。我猜是一个 编译器错误然后 调试器。这些位用于设置信号,然后从寄存器中读取信号 以检查按钮是否被按下。 Serve Laurijssen写道,On 05/09/08 14:56: 按产量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<< operatorThis 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 iscode for an 8-bitter? 解决方案By "yield 0" do you mean that the value of idx wasalways zero in "...."? That shouldn''t be so: idx shouldhave been 1, 2, 4, 8 on the four loop iterations. Howdid you discover that idx was zero (or have I notunderstood your complaint)?What were your expectations, and how did you verifythat they were met? In this loop, both idx and mask shouldhave the values 1, 2, 4, 8 during "...." in each iteration,and mask should be 16 after the loop ends.First tell us more about your intentions, and about yourmeans of discovering what your code fragments are doing.--Eric Sosman es*****@ieee-dot-org.invalid By "yield 0" do you mean that the value of idx wasalways zero in "...."? That shouldn''t be so: idx shouldhave been 1, 2, 4, 8 on the four loop iterations. Howdid you discover that idx was zero (or have I notunderstood your complaint)?That was what I meant yes. It was seen with a debugger. I''m guessing acompiler bug thena debugger. The bits were used to set a signal and after that read a signalfrom a register to check if a button was pressed. By "yield 0" do you mean that the value of idx wasalways zero in "...."? That shouldn''t be so: idx shouldhave been 1, 2, 4, 8 on the four loop iterations. Howdid you discover that idx was zero (or have I notunderstood your complaint)?That was what I meant yes. It was seen with a debugger. I''m guessing acompiler bug thena debugger. The bits were used to set a signal and after that read asignal from a register to check if a button was pressed.Did you use the value of idx? I''m guessing possibly not, or possibly itwas only assigned to what C thought of as a variable but was actuallymemory mapped HW. Check that you have used ''volatile'' for thedeclaration/definition of anything which is mapped to HW.I''ve implemented scanning a hex keypad in SW before, strobing the fourslines checking for whether any of the relevant 4 bits are set, and I''mguessing this is what you are doing. I was doing it because I waswriting SW to test the keypad, normally I would just use a dedicatedchip to do it.--Flash Gordon 这篇关于bitshift运算符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 00:27