简体   繁体   English

“int mask =〜0;”的目的是什么?

[英]What is the purpose of “int mask = ~0;”?

I saw the following line of code here in C. 我看到下面的代码行这里的C.

 int mask = ~0;

I have printed the value of mask in C and C++. 我在C和C ++中打印了mask的值。 It always prints -1 . 它始终打印-1

So I do have some questions: 所以我确实有一些问题:

  • Why assigning value ~0 to the mask variable? 为什么价值分配~0面罩变量?
  • What is the purpose of ~0 ? 什么是目的~0
  • Can we use -1 instead of ~0 ? 我们可以使用-1而不是~0吗?

这是一种可移植的方法,可将所有二进制位整数设置为1位,而无需知道当前体系结构中的整数位数。

C and C++ allow 3 different signed integer formats: sign-magnitude, one's complement and two's complement C和C ++允许3种不同的有符号整数格式:符号幅度,1的补码和2的补码

~0 will produce all-one bits regardless of the sign format the system uses. ~0不管该系统采用符号格式的产生全1比特。 So it's more portable than -1 所以它比-1 更便携

You can add the U suffix (ie -1U ) to generate an all-one bit pattern portably 1 . 您可以添加U后缀(即-1U )以便可移植地生成全1位模式1 However ~0 indicates the intention clearer : invert all the bits in the value 0 whereas -1 will show that a value of minus one is needed, not its binary representation 但是~0 表示意图更清楚 :反转值0中的所有位,而-1表示需要减去1的值,而不是其二进制表示

1 because unsigned operations are always reduced modulo the number that is one greater than the largest value that can be represented by the resulting type 1因为无符号运算总是以模数的形式减少,该数字大于结果类型可以表示的最大值

在2的补码平台(假设)上给你-1,但是直接写-1是规则禁止的(只有整数0..255,一元!~和二进制&^|+<<>>被允许)。

You are studying a coding challenge with a number of restrictions on operators and language constructions to perform given tasks. 您正在研究编码挑战,其中包含对运算符和语言结构的许多限制,以执行给定的任务。

The first problem is return the value -1 without the use of the - operator. 第一个问题是返回值-1而不使用-运算符。

On machines that represent negative numbers with two's complement, the value -1 is represented with all bits set to 1 , so ~0 evaluates to -1 : 在使用二进制补码表示负数的机器上,值-1表示所有位设置为1 ,因此~0计算结果为-1

/* 
 * minusOne - return a value of -1 
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int minusOne(void) {
  // ~0 = 111...111 = -1
  return ~0;
}

Other problems in the file are not always implemented correctly. 文件中的其他问题并不总是正确实现。 The second problem, returning a boolean value representing the fact the an int value would fit in a 16 bit signed short has a flaw: 第二个问题,返回一个布尔值,表示一个int值适合16位签名short的事实有一个缺陷:

/* 
 * fitsShort - return 1 if x can be represented as a 
 *   16-bit, two's complement integer.
 *   Examples: fitsShort(33000) = 0, fitsShort(-32768) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 8
 *   Rating: 1
 */
int fitsShort(int x) {
  /* 
   * after left shift 16 and right shift 16, the left 16 of x is 00000..00 or 111...1111
   * so after shift, if x remains the same, then it means that x can be represent as 16-bit
  */
  return !(((x << 16) >> 16) ^ x); 
}

Left shifting a negative value or a number whose shifted value is beyond the range of int has undefined behavior, right shifting a negative value is implementation defined, so the above solution is incorrect (although it is probably the expected solution). 左移一个负值或移位值超出int范围的数字具有未定义的行为,右移一个负值是实现定义的,因此上述解决方案是不正确的(尽管它可能是预期的解决方案)。

Loooong ago this was how you saved memory on extremely limited equipment such as the 1K ZX 80 or ZX 81 computer. Loooong之前,这就是你在1K ZX 80或ZX 81计算机等极其有限的设备上节省内存的方法。 In BASIC, you would 在BASIC中,你会的

Let X = NOT PI

rather than 而不是

LET X = 0

Since numbers were stored as 4 byte floating points, the latter takes 2 bytes more than the first NOT PI alternative, where each of NOT and PI takes up a single byte. 由于数字存储为4字节浮点,后者比第一个NOT PI替代方案多2个字节,其中NOT和PI中的每一个占用一个字节。

There are multiple ways of encoding numbers across all computer architectures. 在所有计算机体系结构中有多种编码方式的方法。 When using 2's complement this will always be true: ~0 == -1 . 当使用2的补码时,这总是正确的: ~0 == -1 On the other hand, some computers use 1's complement for encoding negative numbers for which the above example is untrue, because ~0 == -0 . 另一方面,一些计算机使用1的补码来编码负数,上面的例子是不正确的,因为~0 == -0 Yup, 1s complement has negative zero, and that is why it is not very intuitive. 是的,1s补码有负零,这就是为什么它不是很直观。

So to your questions 所以对你的问题

  • the ~0 is assigned to mask so all the bits in mask are equal 1 -> making mask & sth == sth 〜0被分配给掩码,因此掩码中的所有位都相等1 - >使mask & sth == sth
  • the ~0 is used to make all bits equal to 1 regardless of the platform used 无论使用何种平台,〜0用于使所有位等于1
  • you can use -1 instead of ~0 if you are sure that your computer platform uses 2's complement number encoding 如果您确定您的计算机平台使用2的补码编码,则可以使用-1而不是〜0

My personal thought - make your code as much platform-independent as you can. 我个人的想法 - 尽可能地使您的代码与平台无关。 The cost is relatively small and the code becomes fail proof 成本相对较小,代码变得失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM