Is rightexpert signed int 8 * sizeof (int) or more undefined?

I know this is undefined:

uint32_t u = 1;
u << 32;

But I'm a little confused about what kinds of shifts are undefined.

Is it undefined to shift a signed integer in size (in bits) or more to the right?

Update: As indicated in the answer, it is about the size in bits, not in bytes.

+4
source share
3 answers

sizeof (int)- size intin bytes, so it does not matter. What value is not the size, but the width, which is the number of bits of the value in the view (plus the signed bit for the signed types).

<< >> , undefined. (, short, int , ).

<< , .

>> , .

6.5.7 C ( N1570, C11).

:

. - . , undefined.

E1 << E2 E1 E2 ; . E1 , E1 × 2 E2 , , . E1 , E1 × 2 E2 , ; undefined.

E1 >> E2 E1 E2. E1 , E1 , E1/ 2 E2. E1 , .

+6

. 6.5.7p3

3) . - . , undefined.

, , . .

+2

C , C89, C89 , , .

, (, , -1 4294967295 , ) , . , .

'-, , , , , - . -, , , , , - , - , .

, , - :

unsigned long rotate_left(unsigned long dat, int amount)
{ return (dat << amount) | (dat >> (32-amount)); }

-, "dat", == 0 [ , ] . , C89 , , X C89 , , . , , , , .

0

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


All Articles