• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

danchisholm

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have any exercises on left,right,unsigned right bit manipulations? Did you add Wrapper classes exercise in your web site.
If so please give me web site address.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suresh,
I added the java.lang.Byte wrapper class to my exam on August 9. Click on the link in my signature and then click "Latest Exam" -> "Exam, August 9" -> "Topic Exams". Then click on "Questions" under the "java.lang.Byte" heading.
I will add more wrapper classes to the exam later in the week.
The questions that make use of the right, left, and unsigned right shift operators are in the "operators" exam.
Of course, the above questions are also contained in the comprehensive exams.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class W {
public static void main (String[] args) {
byte x = 3;
byte y = 5;
System.out.print((-x == ~x + 1)+","+(-y == ~y + 1));
}
}
could you explain about this in detail for me. I know only thing is inversion will convert 0 bits to 1 and vice versa.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does it do when you compile it?
What does it produce when you run it?
By looking at the output (or compiler error) you should be able to get a start on the problem.
What operation "==" or "+" has higher priority?
Consider the sentence "flip the bits and add one". That's an algorithm - for what?
-Barry
[ August 14, 2002: Message edited by: Barry Gaunt ]
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Output is true,true
How and Why its output is true, true.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes .. inversion will convert 0s to 1s.
see this ..
byte x is represented as 00000011.
byte y is represented as 00000101.
when you invert the byte x in (-x== ~x+1)
before inversion , x is promoted to int, that is 3 more bytes extending the sign bit at left most bit of x .
so x becomes
00000000 00000000 00000000 00000011
the result of inversion is
11111111 11111111 11111111 11111100
which is -4 ,
that is -3 == -4 +1;
is true.
same is the case with y..
I hope that helps..
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, still editing the post - please read it once more.
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Binu, you have spoiled my "teaching" experience!

I was trying to "lead" through the problem solving.
- Never mind
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when one of your students ( his name is Binu )got a chance to teach somebody .. , he just did it .. but forgot to teach him the way you handled it ..

Now onwords .. I will take care of it ..
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Binu, I am very happy that you did. May the force go with you...
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Suresh! Sorry, everything clear now?
-Barry
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte x = 3
00000000 00000000 00000000 00000011
the result of inversion is
11111111 11111111 11111111 11111100
here x becomes 4 because we are adding
if i assume
1 | 1 | 1 | 1 = 4
11111111 11111111 11111111 11111100
or
get the decimal of 00000011 = 4
I promise this is my last doubt on inversions.
By the way I enjoyed reading both of your conversations as answer to my question
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the result of
~x will not affect x
it returns the value of inversion .
similar to -x will not affect value of x
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
00000000 00000000 00000000 00000011
the result of inversion is
11111111 11111111 11111111 11111100
which is -4 ,
How did you get -4 here.
becuase of this
11111111 = 1
11111111 = 1
11111111 = 1
11111100 = 1
----
4
---
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java uses 2s complement representation for negative value.
for representing a negative value , invert and add one.
say you want to represent -4 in bits.
step1 : write binary representation of 4
00000000 00000000 00000000 00000100
step2 : invert it
11111111 11111111 11111111 11111011
step3 : add 1
11111111 11111111 11111111 11111100
is the representation of -4.
If you want to know the actual value of such a 2s complement representation
say you want to know what the actual value of
11111111 11111111 11111111 11111100
step1 : invert it
00000000 00000000 00000000 00000011
step2 : add 1
00000000 00000000 00000000 00000100
step3 : take the value
4
step4 : put a minus sign
-4.
I hope that helps ..
pls correct me if I am wrong in any step
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Binu, you are correct... That is the meaning of the incarnation "flip the bits and add one", or in a less elegant way "~x + 1".
You also see that some times you have to move in small steps (bits ) as you guide someone back to the Way.
You may continue, in confidence.
Suresh, how ya doing?
-Barry
[ August 14, 2002: Message edited by: Barry Gaunt ]
[ August 14, 2002: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suresh, I send you on a journey to binary enlightenment.
Do not stay long, and take only what you need.
When you are ready, return, Binu and I will be waiting.
[ August 14, 2002: Message edited by: Barry Gaunt ]
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic