← Back to context Comment by uecker 2 months ago It is not even possible to do arithmetic on char in C. 3 comments uecker Reply pjmlp 2 months ago #include <stdio.h> unsigned int pack_rgb(unsigned char r, unsigned char g, unsigned char b) { return (r << 16) | (g << 8) | b; } unsigned int pack_rgb_arith(unsigned char r, unsigned char g, unsigned char b) { return (r * 65536) + (g * 256) + b; } int main(void) { printf("The color value of (246, 176, 223) is %d\n", pack_rgb(246, 176, 223)); printf("The color value of (246, 176, 223) is %d\n", pack_rgb_arith(246, 176, 223)); } Compiler Explorer link, https://godbolt.org/z/3jExdaTT9I would expect a better comment from someone working on the standard. uecker 2 months ago You should know that the type is promoted to int first, which is also what makes your example work. This is what happens when you perform the computation using an non-promoting 8 bit unsigned type: https://godbolt.org/z/fxxva4nWq 3836293648 2 months ago Don't they get promoted to short?
pjmlp 2 months ago #include <stdio.h> unsigned int pack_rgb(unsigned char r, unsigned char g, unsigned char b) { return (r << 16) | (g << 8) | b; } unsigned int pack_rgb_arith(unsigned char r, unsigned char g, unsigned char b) { return (r * 65536) + (g * 256) + b; } int main(void) { printf("The color value of (246, 176, 223) is %d\n", pack_rgb(246, 176, 223)); printf("The color value of (246, 176, 223) is %d\n", pack_rgb_arith(246, 176, 223)); } Compiler Explorer link, https://godbolt.org/z/3jExdaTT9I would expect a better comment from someone working on the standard. uecker 2 months ago You should know that the type is promoted to int first, which is also what makes your example work. This is what happens when you perform the computation using an non-promoting 8 bit unsigned type: https://godbolt.org/z/fxxva4nWq 3836293648 2 months ago Don't they get promoted to short?
uecker 2 months ago You should know that the type is promoted to int first, which is also what makes your example work. This is what happens when you perform the computation using an non-promoting 8 bit unsigned type: https://godbolt.org/z/fxxva4nWq 3836293648 2 months ago Don't they get promoted to short?
Compiler Explorer link, https://godbolt.org/z/3jExdaTT9
I would expect a better comment from someone working on the standard.
You should know that the type is promoted to int first, which is also what makes your example work. This is what happens when you perform the computation using an non-promoting 8 bit unsigned type: https://godbolt.org/z/fxxva4nWq
Don't they get promoted to short?