AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Operators available to byte, short, char types variables

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
lonelyplanet999
Guest





PostPosted: Wed Mar 03, 2004 4:10 am    Post subject: Operators available to byte, short, char types variables Reply with 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.

:)
Back to top
Johann Weber
Guest





PostPosted: Wed Mar 03, 2004 10:00 am    Post subject: Re: Operators available to byte, short, char types variables Reply with quote



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





PostPosted: Wed Mar 03, 2004 5:28 pm    Post subject: Re: Operators available to byte, short, char types variables Reply with quote



[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





PostPosted: Thu Mar 04, 2004 5:29 am    Post subject: Re: Operators available to byte, short, char types variables Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.