• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can u please differentiate these two operators? I got bit confused

Regards,
Anjana
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
~ 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be more specific, the ~ operator can ONLY be applied to integers (byte, short, char, int and long).
Sashi
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sasikanth Malladi:
To be more specific, the ~ operator can ONLY be applied to integers (byte, short, char, int and long).
Sashi


Good point. I should have said "primitive integral values."
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic