Assigned value has more bits and then lvalue value

Just out of curiosity. I want to know what happens when an lvalue has fewer bits than the assigned value.

char a = 0xffffFFFF 

Also say

 struct { int num:3; }testStruct; testStruct.num = 0xffffFFFF/2 -1; 

what will be the value in char (first case) and a 3-bit integer (in the second case)

+5
source share
3 answers

You are using the term "lvalue" incorrectly. You probably mean “variable” or “object”.

In the case of sizeof(0xFFFFFFFF) == 1 and plain char unsigned , then char a = 0xffffFFFF; simple, since this value can be represented in char .

In other cases, this conversion is out of range. If a plain char is unsigned, this means that a large positive number 0xFFFFFFFF configured modulo UCHAR_MAX+1 to match (on common systems, a regular char gets 255 ).

If a plain char is signed, then this is the behavior defined by the implementation (and in C, it can raise the signal defined by the implementation). The most common implementation definition is a two-pad truncation that would give char a value of -1 , but this is not guaranteed.


Moving on a bitfield. 0xffffFFFF/2 - 1 - 0x7FFFFFFE . This is assigned to a bit field of type int and width 3 , which requests a conversion out of range, as in the signed case of char . As before, this is the definition of the implementation (and may raise the signal defined by the implementation by C).

Check your compiler documentation for what it sets here, although I would again expect it to use 2 add-on truncations, giving -2 .

+6
source

The value is saved if the recipient can save the value, otherwise it will be reduced modulo CHAR_MAX + 1 if the destination is not signed, otherwise the result will be determined by implementation (C allows capture).

char could store 0xFFFFFFFF anyway if CHAR_BIT is big enough.


C ++ 14 (n3936):

3.9.1 Basic types [basic.fundamental]

[...] In any particular implementation, a simple char object can take the same values ​​as signed char or unsigned char ; which one is determined by implementation.

4.7 Integral Transforms [conv.integral]

1 Prvalue of an integer type can be converted to a prvalue of another integer type. The prvalue of an unenumerated enumeration type can be converted to a prvalue of an integer type.
2 If the destination type is unsigned , the resulting value is the smallest unsigned integer comparable to the original integer (modulo 2n, where n is the number of bits used to represent the unsigned type).
3 If the destination type is signed , the value does not change if it can be represented in the destination type (and bit width); otherwise, the value is determined by the implementation. 4 If the destination type is bool, see 4.12. If the source type is bool , false converted to zero, and true converted to one.
5 Conversions permitted as integral promotions are excluded from the set of integral transforms.


C has rules that have almost the same result, although observe that the latter case can catch a trap.
C11 (Amendments C99 +) (n1570):

6.2.5 Types

15 The three types of char , signed char and unsigned char are collectively called character types. The implementation should define char for the same range, view and behavior as either signed char or unsigned char .45)

6.3.1.3 Integer and unsigned integers

1 When a value with an integer type is converted to another integer type other than _Bool , if the value can be represented by a new type, it does not change.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one greater than the maximum value that can be represented in the new type until the value is in the range of the new type. 60)
3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is determined by the implementation , and the signal determined by the implementation increases .

+3
source

If the value is not suitable for the integral type (int, char, short, long, etc.):

  • Signed . The behavior is determined by implementation.

  • Unsigned : the modular value is copied. (i.e., higher bits that cannot be stored are ignored when copying)

Note that the char used in your answer may behave like a signed char or unsigned char depending on your compiler and compilation flags.

what will be the value in char (the first case) and the 3-bit integer (in the second case)

The answer depends on the implementation. Check your compiler's documentation for the exact answer that is appropriate for your environment.

+3
source

Source: https://habr.com/ru/post/1210028/


All Articles