这个“if(kc[3] & c)”是什么?部分代码在做什么?

发布于 2024-11-07 20:07:38 字数 455 浏览 3 评论 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;
}
#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你穿错了嫁妆 2024-11-14 20:07:38
if(k.c[2] & c)

这称为按位与。

按位与的说明

   //illustration : mathematics of bitwise AND
   a = 10110101 (binary representation)
   b = 10011010 (binary representation)

   c = a & b 
     = 10110101 & 10011010 
     = 10010000 (binary representation)
     = 128 + 16 (decimal)
     = 144 (decimal)

按位与使用此真值表:

X | Y | R = X & Y
---------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1

请参阅有关按位与的这些教程:

if(k.c[2] & c)

That is called bitwise AND.

Illustration of bitwise AND

   //illustration : mathematics of bitwise AND
   a = 10110101 (binary representation)
   b = 10011010 (binary representation)

   c = a & b 
     = 10110101 & 10011010 
     = 10010000 (binary representation)
     = 128 + 16 (decimal)
     = 144 (decimal)

Bitwise AND uses this truth table:

X | Y | R = X & Y
---------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1

See these tutorials on bitwise AND:

终难愈 2024-11-14 20:07:38

按位运算(本例中为 AND)在 2 个操作数之间执行逐位运算。
例如 & :

11010010 &
11000110 =
11000010

A bitwise operation (AND in this case) perform a bit by bit operation between the 2 operands.
For example the & :

11010010 &
11000110 =
11000010
嘿看小鸭子会跑 2024-11-14 20:07:38

代码中的按位运算

c = 128 因此二进制表示为

c = 10000000

a & c 将会和每 ith 但如果 c 具有 a 的第 ith 位。因为c在MSB位置(第7位)只有1,所以a &如果a在其位置7位有一个1,如果a有一个0,则c将非零 在 pos 位,然后 a & c 将为零。此逻辑用在上面的 if 块中。 if 块的输入取决于字节的 MSB(位置 7 位)是否为 1。

假设 a = ? ? ? ? ? ? ? ? 其中 ?01
然后

a = ? ? ? ? ? ? ? ? 
AND & & & & & & & &
c = 1 0 0 0 0 0 0 0
    ---------------
    ? 0 0 0 0 0 0 0

作为 0 & ? = 0 。因此,如果位位置 7 为 0,则答案为 0;如果位位置 7 为 1,则答案为 1。

在每次迭代中,c 左移一位,因此 1c 中向左传播。因此,在每次使用其他变量进行迭代掩码时,您都可以知道变量的该位置是否有 10

在代码中使用

union temp
{
  float f;
  char c[4];
} k;

在联合内部,floatchar c[4] 共享相同内存位置(作为 union 的属性) )。
现在,sizeof (f) = 4bytes) 您可以分配kf = 5345341 或其他任何值。当您访问数组k.arr[0]时,它将访问浮点f的第0个字节,当您执行k.arr[1]时code> 它访问 float f 的第一个字节。该数组不为空,因为浮点数和数组都指向相同的内存位置,但访问方式不同。这实际上是一种按字节访问 float 的 4 个字节的机制。
注意,k.arr[0] 可能会寻址最后一个字节而不是第一个字节(如上所述),这取决于内存中存储的字节顺序(请参阅小端和大端字节顺序this)

               Union k
+--------+--------+--------+--------+   --+
| arr[0] | arr[1] | arr[2] | arr[3] |     |
+--------+--------+--------+--------+     |---> Shares same location (in little endian)
|              float f              |     |
+-----------------------------------+   --+

或者字节顺序可以颠倒

               Union k
+--------+--------+--------+--------+   --+
| arr[3] | arr[2] | arr[1] | arr[0] |     |
+--------+--------+--------+--------+     |---> Shares same location (in big endian)
|              float f              |     |
+-----------------------------------+   --+

您的代码在此循环并移动 c,它从位 7 传播 c 中唯一的 1在每个位置一次一步到位 0,按位与实际上检查浮点变量 f 字节的每个位位置,如果是 1,则打印 1,否则打印 0

。你打印浮点数的所有 4 个字节,然后你可以看到 IEEE 754 表示。

Bitwise Operation in your code

c = 128 therefore the binary representation is

c = 10000000

a & c will and every ith but if c with evert ith bit of a. Because c only has 1 in the MSB position (pos 7), so a & c will be non-zero if a has a 1 in its position 7 bit, if a has a 0 in pos bit, then a & c will be zero. This logic is used in the if block above. The if block is entered depending upon if the MSB (position 7 bit) of the byte is 1 or not.

Suppose a = ? ? ? ? ? ? ? ? where a ? is either 0 or 1
Then

a = ? ? ? ? ? ? ? ? 
AND & & & & & & & &
c = 1 0 0 0 0 0 0 0
    ---------------
    ? 0 0 0 0 0 0 0

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 the 1 in the c propagates left wise. So in each iteration masking with the other variable you are able to know if there is a 1 or a 0 at that position of the variable.

Use in your code

You have

union temp
{
  float f;
  char c[4];
} k;

Inside the union the float and the char c[4] share the same memory location (as the property of union).
Now, sizeof (f) = 4bytes) You assign k.f = 5345341 or whatever . When you access the array k.arr[0] it will access the 0th byte of the float f, when you do k.arr[1] it access the 1st byte of the float f . 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)

               Union k
+--------+--------+--------+--------+   --+
| arr[0] | arr[1] | arr[2] | arr[3] |     |
+--------+--------+--------+--------+     |---> Shares same location (in little endian)
|              float f              |     |
+-----------------------------------+   --+

Or the byte ordering could be reversed

               Union k
+--------+--------+--------+--------+   --+
| arr[3] | arr[2] | arr[1] | arr[0] |     |
+--------+--------+--------+--------+     |---> Shares same location (in big endian)
|              float f              |     |
+-----------------------------------+   --+

Your code loops on this and shifts the c which propagates the only 1 in the c 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 variable f, 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.

久隐师 2024-11-14 20:07:38

c 中有一个位。 128 是二进制的10000000if(kc[2] & c) 检查该位是否也在 kc[2] 中设置。然后将 c 中的位进行移位以检查其他位。

结果,程序被用来显示浮点数的二进制表示形式。

c has single bit in it set. 128 is 10000000 in binary. if(k.c[2] & c) checks if that bit is set in k.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文