4

Can anyone please let me know what exactly 1LL and -1LL are? Here is the line in which they have been used:

#define All_In(G) (((G) == 64) ? (W64)(-1LL) : ((1LL << (G))-1LL))
user2517676
  • 969
  • 6
  • 15
  • 25
  • 8
    Literal for `long long`. – Rapptz Aug 28 '13 at 22:52
  • So, this line means what? I still can not get its meaning!! – user2517676 Aug 28 '13 at 22:56
  • 3
    It's trying to minimize the chance that there will be an overflow. Because `(1 << G)` will tend to overflow a lot sooner than `(1LL << G)`, because `1` is of type `int`, which is generally smaller than `long long`. – Benjamin Lindley Aug 28 '13 at 23:00
  • `1` is of type `int`. `1L` is of type `long int`. `1LL` is of type `long long` int (a type introduced to the C standard in 1999 and to the C++ standard in 2011, but commonly provided before that as an extension). The sizes of these types vary from one compiler to another, but `long long` is required to be at least 64 bits (though I've seen a *non-conforming* C compiler with a 32-bit `long long` type). Since shifts can be better behaved on unsigned types, I might have used `1ULL` rather than `1LL`. – Keith Thompson Aug 28 '13 at 23:53
  • Described how the macro works step by step – Csaba Toth Aug 29 '13 at 00:13
  • Other example for const literal typing: `1.0f`. So the number 1 has to be interpreted as a single precision floating point number. – Csaba Toth Aug 29 '13 at 00:15

4 Answers4

5

The purpose of the macro appears to be to produce an integer with the G least significant bits set. All_In(1)==1, All_In(2)==3, All_In(3)==7, and so on.

In psuedo-code, the macro is saying this:

if G == 64
    produce -1
else
    produce (1 bitshifted left by G) - 1

bit shifting, if you don't know what that is

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • … but it produces integer overflow for G = 63. To correct, the literal should be `ull`, which makes the whole exercise a bit ironic. – Potatoswatter Aug 29 '13 at 03:18
3

LL stands for LongLong, which means at least 64-bit

Kurt Pattyn
  • 2,758
  • 2
  • 30
  • 42
  • 7
    It doesn't "mean 64 bits", though. It just means "long long". – Kerrek SB Aug 28 '13 at 22:54
  • 7
    Though it does imply(or rather, require) *at least* 64 bits. – Benjamin Lindley Aug 28 '13 at 22:56
  • (further to Benjamin's comment, see pages 21 & 22 of the C99 spec: "... implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign. ... minimum value for an object of type long long int `LLONG_MIN -9223372036854775807//−(2^63−1)`)" – Tommy Aug 28 '13 at 22:58
  • Can anyone please translate what exactly the code line means? Because I still do not get it!!! – user2517676 Aug 28 '13 at 23:00
  • 1
    @user2517676: Then you should ask about the line of code (in your question), not just `LL`. – Cornstalks Aug 28 '13 at 23:09
3

LL suffix of a constant literal means that the literal's type need to be interpreted as long long (signed). To answer exactly the question title: 1LL is a constant literal, which value is 1 and it's type is long long. Similarly, -1LL is -1 with the type long long.

You cannot pass 1LL literal simply into functions which accept integer values of smaller types like long, short. Probably where the All_In macro is used the expected parameter is long long.

All_In(1)  == 0b00000000...00000001LL
All_In(2)  == 0b00000000...00000011LL
All_In(3)  == 0b00000000...00000111LL
All_In(4)  == 0b00000000...00001111LL
All_In(5)  == 0b00000000...00011111LL
All_In(6)  == 0b00000000...00111111LL
All_In(7)  == 0b00000000...01111111LL
All_In(8)  == 0b00000000...11111111LL
...
All_In(57) == 0b00000001...11111111LL
All_In(58) == 0b00000011...11111111LL
All_In(59) == 0b00000111...11111111LL
All_In(60) == 0b00001111...11111111LL
All_In(61) == 0b00011111...11111111LL
All_In(62) == 0b00111111...11111111LL
All_In(63) == 0b01111111...11111111LL
All_In(64) == 0b11111111...11111111LL

The macro works this way:

  1. If the input parameter is 64 ((G) == 64) ? see ternary operator ?:), then it yields a number which binary representation is 64 1 ((W64)(-1LL), where W64 specifies that the width of the number is 64 in bits). For this you need to know, that in case of signed integers and in 2 complement number representation the -1 means all bits set to 1. For example in case of a signed char, the values range from -128 to 127. The binary representation of the -128 is 11111111. Extend this to 64 bit length.
  2. If the input is not 64, then it yields a number which has as many 1s in the binary representation as it is specified: 1LL << (G))-1LL. This goes the following way, let's do it for input 3 and if the bit length is 8.

a. First it shifts the 1 by 3.

0b00000001 (2^0)
0b00000010 (2^1 after shifting by 1)
0b00000100 (2^2 after shifting by 1 again)
0b00001000 (2^3 after shifting by 1 again, 3 times all together)

b. Then it subtracts 1 from that number. This results in a number we wanted. 2^n-1 always consists of bits of ones without a zero in between them. So 2^3-1=7. Which representation is:

0b00000111

Probably this can be used to mask some flags or something.

Note, that (0b prefix for representing binary literals in my example doesn't work with all C compilers).

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
2

The line of code is a macro that produces from a positive integer less than or equal to 64 a long long with that many 1s in its binary expansion. AllIn(G) equals $2^G - 1$. Darn it, no TeX. So,

AllIn(1)  == 0x0000000000000001LL
AllIn(2)  == 0x0000000000000003LL
AllIn(3)  == 0x0000000000000007LL
AllIn(4)  == 0x000000000000000FLL
AllIn(5)  == 0x0000000000000011LL
...
AllIn(64) == 0xFFFFFFFFFFFFFFFFLL
Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
  • `AllIn(63)` results in undefined behavior; It shifts into the sign bit and then it does `LLONG_MIN-1`. Assuming, of course, that `long long` is 64 bits. – bames53 Aug 28 '13 at 23:19
  • The shift gives `0x8000000000000000`, and subtracting `1` gives `0x7FFFFFFFFFFFFFFF`, assuming a little-endian system. Whether `long long` is 64 bits, or more, doesn't really matter. Still, the macro should have used `LLU` instead. Are there any sign-magnitude systems left? – Eric Jablow Aug 28 '13 at 23:45
  • if `0x8000000000000000` is out of range for signed long long (and it is for a 64 bit long long) then it's UB to create it from one like that. – bames53 Aug 29 '13 at 02:12
  • 16 hex digits is 64 bits. – Eric Jablow Aug 29 '13 at 02:24
  • 1
    `1LL << 63` is equal to 2^63. `LLONG_MAX` is (2^63)-1, therefore 2^63 overflows `long long`'s range and `1LL << 63` is undefined behavior. Then, assuming the implementation produces the value LLONG_MIN, the macro does `LLONG_MIN - 1` which would again be undefined behavior. – bames53 Aug 29 '13 at 03:57
  • By the way, this is not in criticism of your answer; I just happened to observe this fact while reading your answer. – bames53 Aug 29 '13 at 04:01