Перейти к содержанию
    

Но разве не должно быть наоборот - если хоть один операнд знаковый, то результат становится знаковым?

не должно быть наоборот. Результат приводится к большей разрядной сетке.

unsigned имеет больше значащих разрядов чем signed.

Поделиться сообщением


Ссылка на сообщение
Поделиться на другие сайты

Ну написано же в стандарте явно про беззнаковость size_t...

3.3.3.4 The sizeof operator

 

Constraints

 

The sizeof operator shall not be applied to an expression that has

function type or an incomplete type, to the parenthesized name of such

a type, or to an lvalue that designates a bit-field object.

 

Semantics

 

The sizeof operator yields the size (in bytes) of its operand,

which may be an expression or the parenthesized name of a type. The

size is determined from the type of the operand, which is not itself

evaluated. The result is an integer constant.

 

When applied to an operand that has type char , unsigned char , or

signed char , (or a qualified version thereof) the result is 1. When

applied to an operand that has array type, the result is the total

number of bytes in the array./35/ When applied to an operand that has

structure or union type, the result is the total number of bytes in

such an object, including internal and trailing padding.

 

The value of the result is implementation-defined, and its type (an

unsigned integral type) is size_t defined in the <stddef.h> header.

 

 

И про преобразование перед делением:

3.2.1.5 Usual arithmetic conversions

 

Many binary operators that expect operands of arithmetic type cause

conversions and yield result types in a similar way. The purpose is

to yield a common type, which is also the type of the result. This

pattern is called the usual arithmetic conversions: First, if either

operand has type long double, the other operand is converted to long

double . Otherwise, if either operand has type double, the other

operand is converted to double. Otherwise, if either operand has

type float, the other operand is converted to float. Otherwise, the

integral promotions are performed on both operands. Then the

following rules are applied: If either operand has type unsigned long

int, the other operand is converted to unsigned long int.

Otherwise, if one operand has type long int and the other has type

unsigned int, if a long int can represent all values of an unsigned

int, the operand of type unsigned int is converted to long int ; if a

long int cannot represent all the values of an unsigned int, both

operands are converted to unsigned long int. Otherwise, if either

operand has type long int, the other operand is converted to long int.

Otherwise, if either operand has type unsigned int, the other

operand is converted to unsigned int. Otherwise, both operands have

type int.

 

The values of operands and of the results of expressions may be

represented in greater precision and range than that required by the

type; the types are not changed thereby.

Изменено пользователем Genadi Zawidowski

Поделиться сообщением


Ссылка на сообщение
Поделиться на другие сайты

Присоединяйтесь к обсуждению

Вы можете написать сейчас и зарегистрироваться позже. Если у вас есть аккаунт, авторизуйтесь, чтобы опубликовать от имени своего аккаунта.

Гость
Ответить в этой теме...

×   Вставлено с форматированием.   Вставить как обычный текст

  Разрешено использовать не более 75 эмодзи.

×   Ваша ссылка была автоматически встроена.   Отображать как обычную ссылку

×   Ваш предыдущий контент был восстановлен.   Очистить редактор

×   Вы не можете вставлять изображения напрямую. Загружайте или вставляйте изображения по ссылке.

×
×
  • Создать...