• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

~operator

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class sap
{
static int b;
public static void main(String[]args)
{
b=7;
System.out.println("b = 7; ~b = \t\t\t -> " + (~b) );
}
}
The Binary of 7 is 00000111.
after inverting it, it is 11111000.
//The output which is coming is -8.Can anybody explain how an output is coming -8?.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with the ~ operator
here is a tip
what ever be the no.
let's say the no. is x
-(x + 1)
~ 7 = -(7 + 1)
=
-8
~ -8 = -(-8 + 1)
= -(-7)
= 7

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a great JavaRanch write-up on this, called Cat and Mouse Games with Bits: http://www.javaranch.com/campfire/StoryBits.jsp
At the bottom of the page is an applet that shows you exactly what results you'll get with the different bit operators.

Susan
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic