File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bitwise operation or pow method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bitwise operation or pow method" Watch "Bitwise operation or pow method" New topic

Bitwise operation or pow method

janne kallio

Joined: Oct 13, 2003
Posts: 16
Hi J-gurus!
Wondering about how I am supposed to understand the following type of question in one book preparing for JSCP certificate. The question was...
What is the numerical range of a char?
This option was given as one choice -(2^15) to (2^15)-1
My opinion is that I should count above statements as bitwise operation as follows:
0000010 2
^ 0001111 15
0001101 13
But not as Math.pow(2,15)
Am I right?
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Janne,
char is really an unsigned int data type. It can't be negative. char is defined by Unicode notation occupying 2 bytes, which gives you 2 to the power of 16 combinations or valid char values. The values start from 0 and end at (2 ^ 16) - 1.
^ here is just a substitute for power, not an XOR operator.

4.2.1 Integral Types and Values
The values of the integral types are integers in the following ranges:
� For byte, from �128 to 127, inclusive
� For short, from �32768 to 32767, inclusive
� For int, from �2147483648 to 2147483647, inclusive
� For long, from �9223372036854775808 to 9223372036854775807, inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

And this is also from JLS:

� For type char, the default value is the null character, that is, '\u0000'.

And another useful quote from JLS:

3.10.4 Character Literals
A character literal is expressed as a character or an escape sequence, enclosed in ASCII single quotes. (The single-quote, or apostrophe, character is \u0027.)
A character literal is always of type char.
' SingleCharacter '
' EscapeSequence '
InputCharacter but not ' or \
The escape sequences are described in �3.10.6.
As specified in �3.4, the characters CR and LF are never an InputCharacter;
they are recognized as constituting a LineTerminator.
It is a compile-time error for the character following the SingleCharacter or EscapeSequence to be other than a '.
It is a compile-time error for a line terminator to appear after the opening ' and before the closing '.
The following are examples of char literals:
Because Unicode escapes are processed very early, it is not correct to write
'\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (�3.3) and the linefeed becomes a LineTerminator in step 2 (�3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (�3.10.6).
Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.
In C and C++, a character literal may contain representations of more than
one character, but the value of such a character literal is implementation-defined. In the Java programming language, a character literal always represents exactly one character.

[ November 12, 2003: Message edited by: Vad Fogel ]
I agree. Here's the link:
subject: Bitwise operation or pow method
Similar Threads
bitwise operation question
Can someone please explain this problem?
comprehensive exam(kathy/Bert book)
Byte-How to use bitwise operators?