What is the purpose of "int mask = ~ 0;"?

I saw the following line of code here in C.

int mask = ~0; 

I printed the value of mask in C and C ++. It always prints -1 .

So, I have a few questions:

  • Why assign ~0 a mask variable?
  • What is the goal of ~0 ?
  • Is it possible to use -1 instead of ~0 ?
+43
c ++ c bit-manipulation masking bit-masks
Sep 23 '17 at 6:19 06:19
source share
6 answers

This is a portable way to set all binary bits to an integer of 1 bit, not knowing how many bits are in integer size in the current architecture.

+77
Sep 23 '17 at 6:22
source share

C and C ++ allow 3 different signed integer formats: sign-value, one addition and two additions

~0 will produce an all-one bit regardless of the character format the system uses. Therefore it is more portable than -1

Moreover, ~0 indicates the intent cleaner: invert all bits to 0, while -1 will indicate that a minus one value is required, not its binary representation

+36
Sep 23 '17 at 6:23
source share

This on a platform with two additions (presumably) gives you -1, but writing -1 is directly prohibited by the rules (only integers 0..255, unary ! ~ And binary & , ^ , | , + , << and >> ) .

+8
Sep 23 '17 at 6:30
source share

You study the coding problem with a number of restrictions on operators and language constructs for performing given tasks.

The first problem is to return -1 without using the - operator.

On machines representing negative numbers with two additions, the value -1 is represented with all bits set to 1 , therefore ~0 is evaluated as -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 performed correctly. The second problem, returning a boolean representing the fact that the value of a int will correspond to the 16-bit character short has a drawback:

 /* * fitsShort - return 1 if x can be represented as a * 16-bit, two 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); } 

The left bias of a negative value or a number whose biased value is outside the range of int has undefined behavior, the right bias of a negative value is determined by the implementation, so the above solution is incorrect (although this is probably expected to be a solution).

+5
Sep 23 '17 at 13:49 on
source share

Loooong back this was how you saved memory on extremely limited hardware such as a 1K ZX 80 or ZX 81 computer. In BASIC you would

 Let X = NOT PI 

but not

 LET X = 0 

Since numbers were stored as 4-byte floats, the latter takes 2 bytes more than the first alternative to NOT PI, where each of NOT and PI occupies one byte.

+3
Sep 23 '17 at 20:17
source share

There are many ways to encode numbers in all computer architectures. When using 2 add-ons, this will always be true: ~0 == -1 . On the other hand, some computers use 1 add-on to encode negative numbers, for which the above example is incorrect, because ~0 == -0 . Yup, the 1s complement has a negative zero, and therefore it is not very intuitive.

So, to your questions

  • assigned a value of 0 to mask, so all the bits in the mask are 1 โ†’ create mask & sth == sth
  • a value of ~ 0 is used so that all bits are equal to 1, regardless of the platform used.
  • you can use -1 instead of ~ 0 if you are sure that your computer platform uses 2 add-on encodings.

My personal thought is to make your code as platform independent as you can. The cost is relatively small and the code becomes fault tolerant

0
Sep 23 '17 at 12:46 on
source share



All Articles