знаковый или беззнаковый?
Feb. 22nd, 2018 12:50 pm(эта запись может быть интересна программистам, знающим C/C++)
Во внутреннем форуме на работе кто-то устроил опрос, который просто не могу не украсть:
[Poll #2078582]
Не смотрите в комментарии перед голосованием, там можно обсуждать правильный ответ.
Во внутреннем форуме на работе кто-то устроил опрос, который просто не могу не украсть:
[Poll #2078582]
Не смотрите в комментарии перед голосованием, там можно обсуждать правильный ответ.
no subject
Date: 2018-02-22 10:59 am (UTC)Может, есть какой-нибудь стандарт С-2017, в котором употребление знака делает выражение знаковым?
no subject
Date: 2018-02-22 11:01 am (UTC)no subject
Date: 2018-02-22 11:03 am (UTC)no subject
Date: 2018-02-22 11:07 am (UTC)no subject
Date: 2018-02-22 11:08 am (UTC)no subject
Date: 2018-02-22 11:11 am (UTC)no subject
Date: 2018-02-22 11:21 am (UTC)http://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_promotion
no subject
Date: 2018-02-22 11:22 am (UTC)The difference between the ANSI C and traditional versions of the conversion rules is that the traditional C rules emphasize preservation of the (un)signedness of a quantity, while ANSI C rules emphasize preservation of its value.
In traditional C, operands of types char, unsigned char, and unsigned short are converted to unsigned int. Operands of types signed char and short are converted to int.
ANSI C converts all char and short operands, whether signed or unsigned, to int. Only operands of type unsigned int , unsigned long, and unsigned long long may remain unsigned.
Мило.
no subject
Date: 2018-02-22 11:22 am (UTC)no subject
Date: 2018-02-22 11:23 am (UTC)no subject
Date: 2018-02-22 11:25 am (UTC)И не говорите, что 16-битных приложений не бывает:)
no subject
Date: 2018-02-22 11:25 am (UTC)no subject
Date: 2018-02-22 11:27 am (UTC)no subject
Date: 2018-02-22 11:33 am (UTC)no subject
Date: 2018-02-22 11:46 am (UTC)unsigned short foo;
foo= 33000;
short a= foo+foo;
В инспекторе вижу:
foo,x: 0x80E8
foo+foo,x: 0x101D0
a: 464
foo: 33000
foo+foo: 66000
Получается, беззнаковое...
no subject
Date: 2018-02-22 11:51 am (UTC)no subject
Date: 2018-02-22 12:01 pm (UTC)Так что висящее в воздухе выражение foo + foo все-таки имеет тип unsigned short.
no subject
Date: 2018-02-22 12:01 pm (UTC)Это должен знать каждый ) [CLion в помощь!]
Date: 2018-02-22 12:09 pm (UTC)no subject
Date: 2018-02-22 12:13 pm (UTC)no subject
Date: 2018-02-22 12:14 pm (UTC)no subject
Date: 2018-02-22 12:14 pm (UTC)no subject
Date: 2018-02-22 12:16 pm (UTC)ПС: а программистом хорошо бы озаботится качеством своих программ, а не забивать голову схоластикой
no subject
Date: 2018-02-22 12:20 pm (UTC)Overflows
Unsigned integer arithmetic is always performed modulo 2^n where n is the number of bits in that particular integer. E.g. for unsigned int, adding one to UINT_MAX gives 0, and subtracting one from 0 gives UINT_MAX.
When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined: it may wrap around according to the rules of the representation (typically 2's complement), it may trap on some platforms or due to compiler options (e.g. -ftrapv in GCC and Clang), or may be completely optimized out by the compiler.
http://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions
Conversion rules
Date: 2018-02-22 12:44 pm (UTC)Но похоже это не так. ОМГ какая клоака прикрыта обычной унарной операцией "++"
Up
Хотя рано паниковать -- ведь в дополнительном коде битовые представления эквивалентны...
ПС
По вопросу:
1. Если sizeof(short) < sizeof(int) => foo+foo signed int
2. Если sizeof(short) == sizeof(int) => foo+foo unsigned int