Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with mask and bitwise manipulator

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49786
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

FYI... this...



looks like an endless loop.

Henry
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21408
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic