File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes problem with mask and bitwise manipulator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with mask and bitwise manipulator" Watch "problem with mask and bitwise manipulator" New topic
Author

problem with mask and bitwise manipulator

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am building a method that needs to:

Assign to a local variable named mask the value 1 shifted left 31 times. This puts a 1 in bit 31 and zeros in all other bits.

Use a for loop to step through all 32 bits, doing the following in each step:
o Use a conditional operator whose condition is (mask & number != 0) to print either 1 or 0.
o After every fourth bit, print a single space to make the output readable.

The last step I am really confused on how to do. Here is my method so far:



So I imagine the last one:

o After every fourth bit, print a single space to make the output readable.

would need to be inside the for loop? If anything in the rest of the method looks wrong please point it out to me.

Thanks a lot for the help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Why are you using a compound assignment operator?
Why didn't you post real code? What you posted is full of compiler errors.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks for the reply, I took a look at my code and tried a couple things to see about making it more real.




you mentioned about the compound assignment operator, how should I write something like that?



Could you please show me where I am wrong? I am having trouble finding info about all the bitwise manipulators operators that actually shows the syntax. All I am getting is lengthy paragraphs describing the concept. So thats why my code is rotten with w/errors, I am taking stabs in the dark.

Thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40


FYI... this...



looks like an endless loop.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks Henry for the response, I am having the hardest time finding info on this topic thats actually useful so I am going at this fairly blind.

You mention about the for loop being endless...


From what I understand I am telling java to:

set mask as a value of 1

repeat loop as long as mask is less than or equal to 32

after each loop shift to the right by one


I am meaning to shift to the right by one byte, I may have my syntax set up wrong so java knows I am meaning to shift to the right by one. It could also be that bitwise manipulators dont count up the same as adding one to a current value with ints. These are areas I am having trouble with. Could you please help me?

Thanks a lot for your help
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Ben Hultin wrote:
I am meaning to shift to the right by one byte, I may have my syntax set up wrong so java knows I am meaning to shift to the right by one. It could also be that bitwise manipulators dont count up the same as adding one to a current value with ints. These are areas I am having trouble with. Could you please help me?


Take the value of one. Shift it right by one. And you get the value of zero.

Take the value of zero. Shift it right by one. And you get the value of zero.

So, if you start with zero, you can shift it right as many times as you like, it will never be greater than 32.

Henry
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
so are you saying that shifting to the right will always result in zero? or shifting to the right with one or zero will result in zero.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Ben Hultin wrote:so are you saying that shifting to the right will always result in zero? or shifting to the right with one or zero will result in zero.



Work it out on paper. It is a basic mathematical concept. And you don't actually need a computer (or a calculator) to do it.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with mask and bitwise manipulator