This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Page 76 Khalid Mughal /***********/ char v1 = ')'; //Unicode value 41 byte v2 = 13; int result1 = ~v1 ; //-42 /****************/ Plz tell me what is Unicode value and how did its complement get -42. I know how to convert Decimal value to Binary and vice-versa but I don't know how to convert Unicode to binary and vice versa. Thanks
Hi, Unicode is a character encoding format. For more info on this please refer Unicode. All Java files are considered as a raw stream of unicode characters. Please see JLS Sec 3.1. So, all the ASCII characters in Java are represented as Unicode escapes, whose values are represented in Hexadecimal format as '\uXXXX'. Here XXXX is the hexadecimal representation of the ASCII character. Lets take your example. char v1 = ')'; ASCII Value for ')' = 41. Corresponding Hexadecmial representation for 41 = 0x29. Unicode representation = '\u0029'; So you could very well write as char v1 = '\u0029'; When you do int result = ~v1; This is what happens. Binary representation of v1 v1 = 0000 0000 0000 0000 0000 0000 0010 1001 ~v1 = 1111 1111 1111 1111 1111 1111 1101 0110 (Invert the bits) And ~v1 = -42 You can verify this result as follows:
Hope this helps. cheers... Ritesh [ April 01, 2004: Message edited by: Ritesh Agrawal ]
Ritesh<br /> <br />SCJP 1.4<br />IBM Test 340<br />IBM AIX V4.0 Certified Professional<br /> <br />Right actions for the future are the best apologies for wrong ones in the past.<br />- Tyron Edwards