What do 0LL or 0x0UL mean?

前端 未结 5 1111
离开以前
离开以前 2020-12-04 21:57

I am reading the Google Go tutorial and saw this in the constants section:

There are no constants like 0LL or 0x0UL

I tried to d

相关标签:
5条回答
  • 2020-12-04 22:36

    +In C-like languages, those suffixes tell you the exact type. So, for example. 9 is an int variable, but 0LL is a long long

    0 讨论(0)
  • 2020-12-04 22:40

    LL designates a literal as a long long and UL designates one as unsigned long and 0x0 is hexadecimal for 0. So 0LL and 0x0UL are an equivalent number but different datatypes; the former is a long long and the latter is an unsigned long.

    There are many of these specifiers:

    1F // float
    1L // long
    1ull // unsigned long long
    1.0 // double
    
    0 讨论(0)
  • 2020-12-04 22:46

    There are several different basic numeric types, and the letters differentiate them:

    0   // normal number is interpreted as int
    0L  // ending with 'L' makes it a long
    0LL // ending with 'LL' makes it long long
    0UL // unsigned long
    
    0.0  // decimal point makes it a double
    0.0f // 'f' makes it a float
    
    0 讨论(0)
  • 2020-12-04 22:54

    0LL is a long long zero.

    0x0UL is an unsigned long zero, expressed using hexadecimal notation. 0x0UL == 0UL.

    0 讨论(0)
  • 2020-12-04 22:57

    These are constants in C and C++. The suffix LL means the constant is of type long long, and UL means unsigned long.

    In general, each L or l represents a long and each U or u represents an unsigned. So, e.g.

    1uLL
    

    means the constant 1 with type unsigned long long.

    This also applies to floating point numbers:

    1.0f    // of type 'float'
    1.0     // of type 'double'
    1.0L    // of type 'long double'
    

    and strings and characters, but they are prefixes:

     'A'   // of type 'char'
    L'A'   // of type 'wchar_t'
    u'A'   // of type 'char16_t' (C++0x only)
    U'A'   // of type 'char32_t' (C++0x only)
    

    In C and C++ the integer constants are evaluated using their original type, which can cause bugs due to integer overflow:

    long long nanosec_wrong = 1000000000 * 600;
    // ^ you'll get '-1295421440' since the constants are of type 'int'
    //   which is usually only 32-bit long, not big enough to hold the result.
    
    long long nanosec_correct = 1000000000LL * 600;
    // ^ you'll correctly get '600000000000' with this
    
    int secs = 600;
    long long nanosec_2 = 1000000000LL * secs;
    // ^ use the '1000000000LL' to ensure the multiplication is done as 'long long's.
    

    In Google Go, all integers are evaluated as big integers (no truncation happens),

        var nanosec_correct int64 = 1000000000 * 600
    

    and there is no "usual arithmetic promotion"

        var b int32 = 600
        var a int64 = 1000000000 * b
        // ^ cannot use 1000000000 * b (type int32) as type int64 in assignment
    

    so the suffixes are not necessary.

    0 讨论(0)
提交回复
热议问题