 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lonelyplanet999 Guest
|
Posted: Wed Mar 03, 2004 4:10 am Post subject: Operators available to byte, short, char types variables |
|
|
Hi,
I would like to ask will <<, >>, >>>, ^, ~ operators be applicable to
above mentioned primitives ? So far I couldn't write code to apply
above operators to mentioned primitive types variables.
:)
|
|
| Back to top |
|
 |
Johann Weber Guest
|
Posted: Wed Mar 03, 2004 10:00 am Post subject: Re: Operators available to byte, short, char types variables |
|
|
lonelyplanet999 wrote:
| Quote: | Hi,
I would like to ask will <<, >>, >>>, ^, ~ operators be applicable to
above mentioned primitives ? So far I couldn't write code to apply
above operators to mentioned primitive types variables.
:)
|
"The type of each of the operands of a shift operator must be a
primitive integral type, or a compile-time error occurs."
"The type of the operand expression of the unary ~ operator must be a
primitive integral type, or a compile-time error occurs"
And of course also the bitwise exclusive or operator ^ is applicable to
all primitive integer types.
I think your problem in your previous post was that you did not cast the
result properly befor assigning it.
Hans
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Wed Mar 03, 2004 5:28 pm Post subject: Re: Operators available to byte, short, char types variables |
|
|
[email]lonelyplanet999 (AT) my-deja (DOT) com[/email] (lonelyplanet999) writes:
| Quote: | I would like to ask will <<, >>, >>>, ^, ~ operators be applicable to
above mentioned primitives ?
|
Yes, but they will be promoted to int. Use masking and casting to get
back to the smaller types (e.g. short s = (short) ((someShort << 3) &&
0xffff)).
|
|
| Back to top |
|
 |
Jon A. Cruz Guest
|
Posted: Thu Mar 04, 2004 5:29 am Post subject: Re: Operators available to byte, short, char types variables |
|
|
Tor Iver Wilhelmsen wrote:
| Quote: | Yes, but they will be promoted to int. Use masking and casting to get
back to the smaller types (e.g. short s = (short) ((someShort << 3) &&
0xffff)).
|
Oops. You made a little mistake there and used the wrong operator.
For bitwise operations, you need only a single ampersand:
short s = (short) ((someShort << 3) & 0xffff))
------------------------------------^
Oh, and there's a little trick I like to use to make the code a little
more readable:
short s = (short) ((someShort << 3) & 0x0ffff))
That leading '0' often clues in readers that there is no sign extension
going on. I use it mainly with bytes, though, and 0x0ff;
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|