integer-promotion

C++ - Bit-wise not of uchar produces int

心已入冬 提交于 2019-11-30 20:22:21
I am surprised by C++'s behavior when applying bit-wise not to an unsigned char. Take the binary value 01010101b , which is 0x55 , or 85 . Applying bit-wise not on an eight bit representation should yield 10101010b , which is 0xAA , or 170 . However, I cannot reproduce the above in C++. The following simple assertion fails. assert(static_cast<unsigned char>(0xAAu) == ~static_cast<unsigned char>(0x55u)); I printed the values of 0x55 , 0xAA , and ~0x55 (as uchar) with the following code. And it reveals that the bit-wise not does not do what I expect it to do. std::cout << "--> 0x55: " << 0x55u <

Does Unary + operator do type conversions?

十年热恋 提交于 2019-11-30 17:29:16
Till now I was believing that there is no use of unary + operator. But then I came across with following example: char ch; short sh; int i; printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i)); // output: 1 2 4 printf("%d %d %d",sizeof(+ch),sizeof(+sh),sizeof(i)); // output: 4 4 4 Does it mean + is doing type conversion here? Because it is behaving same as following printf("%d %d %d",sizeof((int)ch),sizeof((int)sh),sizeof(i)); // output: 4 4 4 This forces me to think + is doing type conversion. But then I try it on double double f; printf("%d %d",sizeof(+f),sizeof((int)f),sizeof(f)); // output:

C++ - Bit-wise not of uchar produces int

有些话、适合烂在心里 提交于 2019-11-30 17:00:05
问题 I am surprised by C++'s behavior when applying bit-wise not to an unsigned char. Take the binary value 01010101b , which is 0x55 , or 85 . Applying bit-wise not on an eight bit representation should yield 10101010b , which is 0xAA , or 170 . However, I cannot reproduce the above in C++. The following simple assertion fails. assert(static_cast<unsigned char>(0xAAu) == ~static_cast<unsigned char>(0x55u)); I printed the values of 0x55 , 0xAA , and ~0x55 (as uchar) with the following code. And it

Truncating an int to char - is it defined?

谁说我不能喝 提交于 2019-11-30 09:02:08
问题 unsigned char a, b; b = something(); a = ~b; A static analyzer complained of truncation in the last line, presumably because b is promoted to int before its bits are flipped and the result will be of type int. I am only interested in the last byte of the promoted int - if b was 0x55, I need a to be 0xAA. My question is, does the C spec say anything about how the truncation happens , or is it implementation defined/undefined? Is it guaranteed that a will always get assigned the value I expect

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

夙愿已清 提交于 2019-11-30 08:38:35
Consider following example: #include <stdio.h> int main(void) { unsigned char a = 15; /* one byte */ unsigned short b = 15; /* two bytes */ unsigned int c = 15; /* four bytes */ long x = -a; /* eight bytes */ printf("%ld\n", x); x = -b; printf("%ld\n", x); x = -c; printf("%ld\n", x); return 0; } To compile I am using GCC 4.4.7 (and it gave me no warnings): gcc -g -std=c99 -pedantic-errors -Wall -W check.c My result is: -15 -15 4294967281 The question is why both unsigned char and unsigned short values are "propagated" correctly to (signed) long , while unsigned int is not ? Is there any

How is shift operator evaluated in C?

泄露秘密 提交于 2019-11-29 16:12:31
问题 I recently noticed a (weird) behavior when I conducted operations using shift >> << ! To explain it, let me write this small runnable code that does two operations which are supposed to be identical(In my understanding), but I'm surprised with different results! #include <stdio.h> int main(void) { unsigned char a=0x05, b=0x05; // first operation a = ((a<<7)>>7); // second operation b <<= 7; b >>= 7; printf("a=%X b=%X\n", a, b); return 0; } When ran, a = 5 and b = 1 . I expect them both to be

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

…衆ロ難τιáo~ 提交于 2019-11-29 12:06:54
问题 Consider following example: #include <stdio.h> int main(void) { unsigned char a = 15; /* one byte */ unsigned short b = 15; /* two bytes */ unsigned int c = 15; /* four bytes */ long x = -a; /* eight bytes */ printf("%ld\n", x); x = -b; printf("%ld\n", x); x = -c; printf("%ld\n", x); return 0; } To compile I am using GCC 4.4.7 (and it gave me no warnings): gcc -g -std=c99 -pedantic-errors -Wall -W check.c My result is: -15 -15 4294967281 The question is why both unsigned char and unsigned

Truncating an int to char - is it defined?

ⅰ亾dé卋堺 提交于 2019-11-29 11:08:37
unsigned char a, b; b = something(); a = ~b; A static analyzer complained of truncation in the last line, presumably because b is promoted to int before its bits are flipped and the result will be of type int. I am only interested in the last byte of the promoted int - if b was 0x55, I need a to be 0xAA. My question is, does the C spec say anything about how the truncation happens , or is it implementation defined/undefined? Is it guaranteed that a will always get assigned the value I expect or could it go wrong on a conforming platform? Of course, casting the result before assigning will

Integral promotion

泪湿孤枕 提交于 2019-11-29 07:25:38
When is it ever the case that a signed integer cannot represent all the values of the original type with regards to integer promotion? From the text K&R, C Programming Language, 2nd Ed. p. 174 A.6.1 Integral Promotion A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer may be used. If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int . This process is called integral promotion. This code

Explain integer comparison with promotion

放肆的年华 提交于 2019-11-29 07:09:27
问题 I'm trying to understand how integer promotion and comparison in and c++ application works. #include <cstdint> int main(void) { uint32_t foo = 20; uint8_t a = 2; uint8_t b = 1; uint8_t c = 5; if(foo == b*c) {} if(foo == a) {} if(foo == a + c) {} if(foo == a + b*c) {} return 0; } Only for the last comparison i get a compiler warning: "comparison between signed and unsigned integer expressions [-Wsign-compare]". Why does this only happen in the last case but not in the others? 回答1: since the