0

Here is this simple code:

char a = '10';
char b = '6';
printf("%d\n", a | b);
printf("%d\n", a ^ b);
printf("%d\n", a << 2);

and the output is

54
6
192

Now my question is, why these results. I checked it on paper and what I have is

1110 for a | b = 14
1100 for a ^ b = 12
00101000 for a << 2 = 40

So why this different result?

Saphire
  • 1,812
  • 1
  • 18
  • 34

3 Answers3

3

You are declaring:

char a = '10';
char b = '6';

In this case b is 00110110 (0x36) because you are declaring a character, not an integer.

I also don't know why char a = '10'; even works because single quotes (') are used to create only single chars literals and you're declaring two there.

The correct way should be:

char a = 10;
char b = 6;
printf("%d\n", a | b);
printf("%d\n", a ^ b);
printf("%d\n", a << 2);
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • 1
    Side note: Some compilers accept integer "multi-character constants" , but they are highly compiler/platform dependent. Compare http://stackoverflow.com/questions/6944730/multiple-characters-in-a-character-constant. – Martin R Feb 21 '14 at 18:58
  • Yes I assumed that. Thanks for clearing it up for me. – Paulo Bu Feb 21 '14 at 18:59
  • 1
    So what is the bit-repr of '10' then? – Saphire Feb 21 '14 at 19:02
  • @Saphire no idea lol :D. I don't know how the compiler will handle this. – Paulo Bu Feb 21 '14 at 19:04
  • 3
    For completeness sake, multi-character constants are implementation-defined, but conforming compilers are required to handle them. Character literals have type `int` and usually compilers map the characters of a multi-character constant to bytes in the `int`. Take a look here for more information: http://stackoverflow.com/questions/6944730/multiple-characters-in-a-character-constant – Matt Eckert Feb 21 '14 at 19:09
  • 1
    @Saphire it's probably 0x3130 or 12592, but since you assign it to a `char` it gets truncated to 0x30 or 48. – Mark Ransom Feb 21 '14 at 19:18
2

You solved this on paper for int 10 and 6 not for chars '10' = 49 or 48 (interpretation of multi-character constant is compiler dependent) and '6' = 54.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • How the multi-character constant `'10'` is interpreted is highly compiler-dependent. For example with Clang on OS X, it is `= 48`, i.e. the first character is ignored. – Martin R Feb 21 '14 at 19:02
  • Why 49? How did you calculate that? – Saphire Feb 21 '14 at 19:05
  • Both are correct @haccks,saphire Its totally compiler dependant. Mostly compilers will interpret this assignment from right to left but some compilers from left to right. – RahulKT Feb 21 '14 at 19:26
1

This is because you defined the variables as character(char) but in the notebook you are calculating the result by treating them as Integer(int) If you want the correct answer try this code and check -

int a = 10; 
int b = 6;
printf("%d\n", a | b);
printf("%d\n", a ^ b);
printf("%d\n", a << 2);
haccks
  • 104,019
  • 25
  • 176
  • 264
RahulKT
  • 171
  • 4