Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# 'k' | 35 (i.e char value | int value)

Sagarika nair
Ranch Hand
Posts: 39
Hi!!
I just wanted to know if such questions can be part of some code in the exam?I haven't encountered any so far in the books but was just curious when I was trying out my own practice code.But if it occurs in the exam,how do we represent a char value in binary form to do the (|)or any bitwise operations.The result of the following expn is 107.
char x='k';
int y=35;
int z=x|y;
System.out.println("x|y val="+z);

Kalai Ganesh
Ranch Hand
Posts: 32
Hai,
I didn't come across any question like this in my exam. They don't expect you to remember the whole bunch of values.
HTH

Cathy Song
Ranch Hand
Posts: 270
Hi,
The example above performs the following
107|35 (Here 107 is the ascii of character 'k')
Even though you dont need to know the ascii values , I think you should
know that in any arithmetic expression involving byte/short/char, they are always widened to an int.
For example:
byte b = 10;
short s = 12;
char c = 'r';
b + s + c = ?
int i = b + s + c;
Another example:
byte b = 3, c = 4;
byte d = b + c; //error since b + c will be widened to an int
So the above error can be fixed in 2 ways
1. byte d = (byte) (b + c);
2. int d = b + c;
I hope this helps.
[ November 21, 2003: Message edited by: Cathy Song ]

Sagarika nair
Ranch Hand
Posts: 39
Thanks Kalai for letting me know that such a question doesn't occur in the exam and thanks Cathy for your explanation eventhough I know about the automatic promotion rules.What I didn't understand is the significance of the promotion rules in my question about char representation.Thanks anyways.

Vivek Nidhi
Ranch Hand
Posts: 133
I think JLS is better catch to know about the most of the automatic conversions friend.
regs
Vivek Nidhi