C 教程中的位运算混淆
如何: 第一: 第二: 数据:C 教程中的位运算混淆,c,C,如何: 第一: 第二: 数据: 该表显示PCIE在位掩码中为0b00100000,因此|=(1它是相等的,因为在或操作之前,这些寄存器的所有其他位都为0 1u短版本 将复合表达式从x |=y分解为x=x | y 加载整数“1”,它只是二进制“[0000 0001],并将其移动到我们想要的位置 变量是在头文件中定义的,或者是在其他地方定义的,说明该位的兼容性。新卡?只需获取一个新的定义文件-这通常是自动完成的)。PCIE和PCINT4是在某个地方定义的,如果需要,可以查找它,但这应该可以为您
该表显示PCIE在位掩码中为0b00100000,因此|=(1它是相等的,因为在
或
操作之前,这些寄存器的所有其他位都为0
1u短版本
GIMSK=GIMSK或0b00100000
和PCMSK=PCMSK或0b00010000代码>
这是相似的,但不是完全相同的事情
GIMSK = 0b00100000;
PCMSK = 0b00010000;
详细解释。
也就是说,最好有人读这篇文章,让我永远无法忘怀
让我们从词汇表的角度来开始,所以我们都在谈论同一件事
GIMSK |=(1你似乎在问两个问题。GIMSK |=(1我在问一个问题:上面的和下面的如何相等。我也不确定你从哪里得到了GIMSK=(1我仍然看到两个问题a)|=
的用法,你问为什么这与=/code>相同,我说,这不一样。b)要获得0b00100000
的值,需要1不,我在哪里问过|=和=以及它们的意思。我试图了解二进制数如何等于1,你问过它们是否相等,因为你的代码在一个地方使用=
,在另一个地方使用=
,它们不是。我还解释了两者之间的区别即使是位位置和位掩码。我明白这意味着什么,我的C语言知识非常有限。从我的理解来看,PCINT4==0b00010000
我错了吗?不知怎的,PCMSK |=(1否,PCINT4
是移位1
的位数,因此它变成了0b00010000
,在这种情况下,PCINT4
是4
。感谢更新P_uj_uuuuuuu。非常有用,我确信我现在理解了。我在做Atmel工作时遇到了一些类似的代码。我正在拼命地回忆答案,我请开发人员解释,他解释了。呃,这让我很难受。我认为这与引脚值被推到引脚的方式有关。你有银行-A B C D(如果更大的话,还有更多)。这与结果输出到管脚和接收的方式有关。每个处理器管脚最多提供8个物理管脚。它通过输出一个字节来实现这一点,我认为每一位代表管脚的开或关(H/L)状态。请检查这一点-一旦我减速,我知道我可以更好地回答这个问题,但现在不行,请将其返回到低。
GIMSK = 0b00100000;
PCMSK = 0b00010000;
The devil is in the details, see below for
GIMSK |= (1 << PCIE);
PCMSK |= (1 << PCINT4);
GIMSK = 0b00100000;
PCMSK = 0b00010000;
GIMSK- variable or 'id'
|= - operator and assignment combo bitwise OR and =
1 - the integer 1
<< - shift operation
PCIE - Variable and ID
x = x + 1 ;this is so common though, that in C, it was shortened to +=
x += 1 ; now its written like this. It takes some time
y = y * 2; it works for other types of operands
y *= 2; Now we take y, multiply by 2 and assign back to y.
GIMSK |= (1 << PCIE)
GIMSK = GIMSK | (1 << PCIE) #OK! much easier to understand if your new.
#NOW we can lexigraphically analyze this
VarA {assignment} VarA OR ( 1 {Operator} VarB )
#Ignore the assignment side, for now, practice order of Operations
#Start with Parenthetical Exp.
1 {Operator} VarB
#It turns out this is defined.
#OP Didnt know but computer does. = 5 in this case.
#so 1, shift left 5. To bitwise shift, need bits
1 => 0b00000001 << 5 = 0b00100000
# shift left is really multiplied by 2 in base 10, divide by 2 in shift right. Beware Right Shift, esp in float.
Register(b) Me OR NOR XOR AND NAND N XNOR
1 0 1 0 1 0 1 0 0
0 0 0 1 0 0 1 1 1
1 1 1 0 0 1 0 0 1
0 1 1 0 1 0 1 1 0
#take a 1,
0000 0001 = $temp
#shift it 5 spots "<<" , where 5 is the PCIE 'bit' value spot number.
1<<5 = 32
#binary equals 32.
# 0010 0000
# Then OR this with whats in the register now:
1010 1010 (made up number, a mix of ones and 0s)
0010 0000 (Our Value)
OR=>
1010 1010 Result.
#Take it bit by bit, no Pun intended
GIMSK |= (1 << PCIE);
PCMSK |= (1 << PCINT4);
GIMSK = 0b00100000;
PCMSK = 0b00010000;
GIMSK- some variable
|= - bitwise OR
1 - the integer 1
`<`< - shift operation
PCIE - another var
PCMSK = 0b00010000; #This sets the PCMSK register to be exactly
=> PCMSK = `0|0|0|1|0|0|0|0
#While
PCMSK = PCMSK OR 0b00010000; # yields PCMSK = `?|?|?|1|?|?|?|?`
#Obviously,
GIMSK = 0b00001000; # This sets the GIMSK register to be exactly
=> GIMSK = `0|0|0|0|1|0|0|0`
While`GIMSK = GIMSK OR 0b00001000; # yields
GIMSK = ` ?|?|?|?|1|?|?|? `