programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

# ~ and !

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
Can u please differentiate these two operators? I got bit confused

Regards,
Anjana

Ranch Hand
Posts: 65
• Number of slices to send:
Optional 'thank-you' note:
~ is a logical operator. Bitwise complement. flips all the bits ( 1s' to 0s' and 0s' to 1s')

! is a conditional operator. ( !a - returns true if a is false )

Anjana Ravindran
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
do u say that ~ applies to numeric and boolean types and ! applies to boolean types? (same as other bitwise and logical operators)

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
Bitwise inversion ( ~ ) is a unary operator (meaning that it acts on a single operand) that simply flips the bits of a primitive numeric value. However, it can not be applied to booleans.

To "flip" a boolean value, use the unary "not" ( ! ), which can be applied only to booleans.

Bitwise operators "and" ( & ), "or" ( | ), and "exclusive or" ( ^ ) are binary operators (meaning that 2 operands are involved) that can be applied to either primitive numerics or booleans.

The short-circuiting versions of "and" and "or" ( && and || ) can be used only with booleans.
[ December 06, 2005: Message edited by: marc weber ]

Ranch Hand
Posts: 175
• Number of slices to send:
Optional 'thank-you' note:
To be more specific, the ~ operator can ONLY be applied to integers (byte, short, char, int and long).
Sashi

marc weber
Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note: