4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

メモ:シフト演算子でも整数拡張は起こる

Last updated at Posted at 2016-04-05

Arduinoのサンプルスケッチを見ていたときに、自分自身の「思い込み」にはまったので、メモ。

#include <stdio.h>
int main(void)
{
    char a = 1;
    printf("sizeof(a) = %d\n", sizeof(a));
    printf("sizeof(a << 4) = %d\n", sizeof(a << 4));
    return 0;
}

を実行すると、

% ./a.out
sizeof(a) = 1
sizeof(a << 4) = 4

となる。

うっかり、char型に収まる範囲でのビットシフトでは型が変化しないと思い込んでしまったが、整数拡張は起こる。

参考: https://www.jpcert.or.jp/sc-rules/c-int02-c.html

整数拡張は通常の算術型変換の一部として、引数式や、単項 +、-、~演算子, シフト演算子のオペランドに対して適用される。

4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?