This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Doubt regarding ~BITWISE NOT

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Bitwise{
public static void main(String arg[])
{
Bitwise b= new Bitwise();
b.abc();
}
public void abc()
{
byte x=13|2;
byte y=~x;
System.out.println(y);
}
}

1101 --->13
|
0010 --->2
---------------
1111 --->15

so x= 15

~ x means invert all so 1111 become 0000
and so out put should be 0

but when i run the programm it is giving me -16 out put how ?
[ January 11, 2007: Message edited by: Akhilesh Yadav ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
primitive byte variable has 8 bits so when you go to OR operation it should be:

0000 1101 ---> 13
|
0000 0010 ---> 2
0000 1111 ---> 15
~
1111 0000 ---> -16
 
Akhilesh Yadav
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see I calculate 13
as 0 0 0 0 1101 ---->13
128643216 8421

add all bit having 1 so it gives 8+4+1=13

as you told
0000 1111 --- >15
~
----------
1111 0000 ==> 280

am i calculating in wrong way please let me know
 
Akhilesh Yadav
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see I calculate 13
as 0 0 0 0 1101 ---->13
128643216 8421

add all bit having 1 so it gives 8+4+1=13

as you told
0000 1111 --- >15
~
----------
1111 0000 ==> 240

am i calculating in wrong way please let me know
[ January 11, 2007: Message edited by: Akhilesh Yadav ]
 
Tanakorn Numrubporn
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every number primitive in java is use the MOST LEFT BIT to represent for sign symbol. This mean the maximum value of each primitive is 2 power (bit number - 1).

Example for byte:
This primitive has totally 8 bits that mean it use 1 bit for represents sign(-/+) and 7 bit for its value.

1 is - and 0 is + for sign bit.

But This is not the whole story. In java language, it use two complement method for convert from positive value to negative.

The method is:
Example
byte x = 13; ---> 0000 1101
byte y = -13; ---> 1000 1101 (Does it correct? NOOO)

-13 is can't be simply convert from 0000 1101 to 1000 1101 but it use two complement method by following.

0000 1101 ---> 13
~
1111 0010 ---> one complement value
+
1
---------
1111 0011 ---> -13

So we return to our problem byte -16

0001 0000 ---> 16
~
1110 1111 ---> one complement value
+
1
---------
1111 0000 ---> -16

So that it. If you don't clear feel free to told me.

Tanakorn
 
Tanakorn Numrubporn
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for confusing indent post now I repost it.




Let me conclude the method
1. we must make a bit wise for that number(in base 2) the result we call it the one complement value.
2. plus 1 to that one complement value.
3. We will get the two complement value that represent the negative value of original value and if you notice the left most bit is 1
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!