15

I'm doing some X11 ctypes coding, I don't know C but need some help understanding this.

In the C code below (might be C++ im not sure) we see (~0L) what does that mean? In Javascript and Python ~0 means -1.

812   int result = GetProperty(window, property_name,
813                            (~0L), // (all of them)
814                            &type, &format, &num_items, &properties);

Thanks

David G
  • 94,763
  • 41
  • 167
  • 253
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • How do I write a short 0, such as 0S? I see: https://stackoverflow.com/questions/208433/how-do-i-write-a-short-literal-in-c – Zhang May 17 '18 at 01:40

2 Answers2

22

0L is a long integer value with all the bits set to zero - that's generally the definition of 0. The ~ means to invert all the bits, which leaves you with a long integer with all the bits set to one.

In two's complement arithmetic (which is almost universal) a signed value with all bits set to one is -1.

The reason for using ~0L instead of -1L is to be clearer about the intent - it's not meant to be used as a number at all, but rather as a collection of bits.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Thank you mark so much! So the number I should use in my ctypes is `11111111111111111111111111111111` if the software is 32bit and 64 1's if its 64bit `1111111111111111111111111111111111111111111111111111111111111111`? – Noitidart Dec 22 '14 at 03:30
  • 2
    The trouble is that if the parameter you're calling is a `long long` then `~0L` only inverts some of the bits, whereas `-1` (no suffix required) inverts all of them. – M.M Dec 22 '14 at 03:35
  • 2
    @Noitidart it depends on your definition of `long`. Microsoft defines it as only 32 bits, even for 64-bit software. – Mark Ransom Dec 22 '14 at 04:04
  • 1
    @Notidart, if it's Python ctypes just use `c_long(~0)`. The size of a `c_long` depends on the platform. – Eryk Sun Dec 22 '14 at 23:00
  • @eryksun I missed your comment back then, I'm glad I saw it now, that worked wonderfully thank you so much! – Noitidart Apr 27 '16 at 23:53
3

Bitwise compliment of zero of long type.

Bill
  • 5,263
  • 6
  • 35
  • 50
  • I'm not a computer science guy just a hobbyist. Haha so that is like alien talk to me :P Is that `-1`? I kind of need a number to put into my ctypes :P – Noitidart Dec 22 '14 at 03:15
  • 3
    @No: Nearly, at least on two's complement implementations: `-1L` Anyway, why not just use the same expression in C++? – Deduplicator Dec 22 '14 at 03:16
  • Thanks @Deduplicator I'm more confused now haha whats two's completment implmentations? haha does that mean 64bit or 32bit software? So if I put `-1` would that be ok? We don't have a `L` in ctypes. – Noitidart Dec 22 '14 at 03:18
  • 2
    It's all bits set to 1. – hatchet - done with SOverflow Dec 22 '14 at 03:18
  • 2
    @Noitidart it can be -1L but you can think of it like this. If a 64bit 0L is 8 bytes all zeros (and each byte's 8 bits are zeros), then the ~0L operation means you take all the 0 bits in those bytes and turn them to 1's. So for each byte that starts as [00000000] in bits, the result is [11111111]. – DNT Dec 22 '14 at 03:19
  • 3
    @Noitidart `~0L` means exact as [Bill](http://stackoverflow.com/a/27596134/2410359) says. To use -1L, etc. is less potable as `-1L` may not have a representation of all bits set. – chux - Reinstate Monica Dec 22 '14 at 03:23
  • Thank you all so much! So the number I should use in my ctypes is `11111111111111111111111111111111` correct? and if 64bit then i should use 64 1's? – Noitidart Dec 22 '14 at 03:29