这个“if(kc[3] & c)”是什么?部分代码在做什么?
#include<stdio.h>
#include<iostream.h>
main()
{
unsigned char c,i;
union temp
{
float f;
char c[4];
} k;
cin>>k.f;
c=128;
for(i=0;i<8;i++)
{
if(k.c[3] & c) cout<<'1';
else cout<<'0';
c=c>>1;
}
c=128;
cout<<'\n';
for(i=0;i<8;i++)
{
if(k.c[2] & c) cout<<'1';
else cout<<'0';
c=c>>1;
}
return 0;
}
#include<stdio.h>
#include<iostream.h>
main()
{
unsigned char c,i;
union temp
{
float f;
char c[4];
} k;
cin>>k.f;
c=128;
for(i=0;i<8;i++)
{
if(k.c[3] & c) cout<<'1';
else cout<<'0';
c=c>>1;
}
c=128;
cout<<'\n';
for(i=0;i<8;i++)
{
if(k.c[2] & c) cout<<'1';
else cout<<'0';
c=c>>1;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这称为按位与。
按位与的说明
按位与使用此真值表:
请参阅有关按位与的这些教程:
That is called bitwise AND.
Illustration of bitwise AND
Bitwise AND uses this truth table:
See these tutorials on bitwise AND:
按位运算(本例中为 AND)在 2 个操作数之间执行逐位运算。
例如 & :
A bitwise operation (AND in this case) perform a bit by bit operation between the 2 operands.
For example the & :
代码中的按位运算
c = 128
因此二进制表示为a & c
将会和每 ith 但如果c
具有a
的第 ith 位。因为c
在MSB位置(第7位)只有1
,所以a &如果
在 pos 位,然后a
在其位置7位有一个1
,如果a
有一个0,则c
将非零a & c
将为零。此逻辑用在上面的if
块中。 if 块的输入取决于字节的 MSB(位置 7 位)是否为 1。假设 a = ? ? ? ? ? ? ? ? 其中
?
是0
或1
然后
作为
0 & ? = 0 。因此,如果位位置 7 为 0,则答案为 0;如果位位置 7 为 1,则答案为 1。
在每次迭代中,
c
左移一位,因此1
在c
中向左传播。因此,在每次使用其他变量进行迭代掩码时,您都可以知道变量的该位置是否有1
或0
。在代码中使用
您
在联合内部,
float
和char c[4]
共享相同内存位置(作为 union 的属性) )。现在,
sizeof (f) = 4bytes)
您可以分配kf = 5345341
或其他任何值。当您访问数组k.arr[0]
时,它将访问浮点f
的第0个字节,当您执行k.arr[1]
时code> 它访问 floatf
的第一个字节。该数组不为空,因为浮点数和数组都指向相同的内存位置,但访问方式不同。这实际上是一种按字节访问 float 的 4 个字节的机制。注意,
k.arr[0]
可能会寻址最后一个字节而不是第一个字节(如上所述),这取决于内存中存储的字节顺序(请参阅小端和大端字节顺序this)或者字节顺序可以颠倒
您的代码在此循环并移动
c
,它从位 7 传播c
中唯一的1
在每个位置一次一步到位 0,按位与实际上检查浮点变量f
字节的每个位位置,如果是 1,则打印 1,否则打印 0。你打印浮点数的所有 4 个字节,然后你可以看到 IEEE 754 表示。
Bitwise Operation in your code
c = 128
therefore the binary representation isa & c
will and every ith but ifc
with evert ith bit ofa
. Becausec
only has1
in the MSB position (pos 7), soa & c
will be non-zero ifa
has a1
in its position 7 bit, ifa
has a0
in pos bit, thena & c
will be zero. This logic is used in theif
block above. Theif
block is entered depending upon if the MSB (position 7 bit) of the byte is 1 or not.Suppose
a = ? ? ? ? ? ? ? ?
where a?
is either0
or1
Then
As
0 & ? = 0
. So if the bit position 7 is 0 then answer is 0 is bit position 7 is 1 then answer is 1.In each iteration
c
is shifted left one position, so the1
in thec
propagates left wise. So in each iteration masking with the other variable you are able to know if there is a1
or a0
at that position of the variable.Use in your code
You have
Inside the union the
float
and thechar c[4]
share the same memory location (as the property of union).Now,
sizeof (f) = 4bytes)
You assignk.f = 5345341
or whatever . When you access the arrayk.arr[0]
it will access the 0th byte of the floatf
, when you dok.arr[1]
it access the 1st byte of the floatf
. The array is not empty as both the float and the array points the same memory location but access differently. This is actually a mechanism to access the 4 bytes of float bytewise.NOTE THAT
k.arr[0]
may address the last byte instead of 1st byte (as told above), this depends on the byte ordering of storage in memory (See little endian and big endian byte ordering for this)Or the byte ordering could be reversed
Your code loops on this and shifts the
c
which propagates the only1
in thec
from bit 7 to bit 0 in one step at a time in each location, and the bitwise anding checks actually every bit position of the bytes of the float variablef
, and prints a 1 if it is 1 else 0.If you print all the 4 bytes of the float, then you can see the IEEE 754 representation.
c
中有一个位。 128 是二进制的10000000
。if(kc[2] & c)
检查该位是否也在kc[2]
中设置。然后将 c 中的位进行移位以检查其他位。结果,程序被用来显示浮点数的二进制表示形式。
c
has single bit in it set. 128 is10000000
in binary.if(k.c[2] & c)
checks if that bit is set ink.c[2]
as well. Then the bit in c is shifted around to check for other bits.As result the program is made to display the binary representation of float it seems.