Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bit values of an integer

 
Brian Fofian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having some trouble with some pretty basic stuff, and I was hoping someone could give me a hand. I need to write a program that asks the user to enter an integer and bit position. The program then needs to write out the value of the bit in that position of the integer (1 or 0). I know this sounds easy but I'm having trouble getting the correct values, especially in the bit position zero. I'm supposed to write it as a single method, and here's what i have so far:


Amatuerish at best I'm sure, but I'm getting correct values for most of my input. But if i input integer 7 at bit position 0, I'm getting that "Bit 0 of 7 is 0" when it should be set at 1. Also if there is a less complicated way of checking the value at a given bit position that might solve my problem as well.
 
Brian Fofian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry put wrong version of code up in last post, this works better but still has problems with zero bit position

 
Norm Radder
Ranch Hand
Posts: 733
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the problem with the latest code you posted? Could you run it a few times, copy the console and paste it here with comments on what is wrong?

Could you explain the algorithm you are trying to implement? That's usually done by puttng comments in the code.

For example what is the purpose of the following?
n = (int)0xAAAAAAAB;
What is the variable b used for?

Variable names can be more than 1 letter long and can be used to document their usage.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's write a method that accepts a bit position and an integer and returns true if its zero. That's the only thing that distinguishes your task from a task that has inputs and outputs.

static public boolean isZero(int position, int value)
{
position -= 1; // zero based position
int mask = 1; // single bit mask

// Move the mask bit in to position
mask <<= position;

// Mask off all bits except for the target
value &= mask;

// If the whole value is zero then the masked bit was zero
return value == 0;
}

Now, to test this use hexadecimal numbers. You can easily tell which bits are zero with a hex number so that's a good format for testing:

public static void main(String[] args)
{
System.out.print(isZero(4, 0xF8));
}
 
Brian Fofian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main problem I'm getting is that it is saying "Bit 0 of 7 is 0" when it should be set at 1. To be honest i was going a different path but saw some code where someone was trying to get bit values from a short and converted that idea to what I am doing. So the reason I'm messed up is because I'm a little confused myself. Int b is storing the original integer inputed by the user. n= (int)0xAAAAAAAB I assume is converting it to 32 bit binary. So as hexdecimal pattern A= 1010 b=1011 then 0xAAAAAAAB must be 1010 1010 1010 1010 1010 1010 1010 1011. I got this idea from a reply to post that i read here:

java forum post

He was doing something similar but with a 16 bit short and therefore his corresponding line of code was


I know there must be a simpler way but this way is the closest I have gotten it to working. Thanks for your help.

-Brian
 
Brian Fofian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rick I'll work more on it using your ideas and get back to you if I have any questions.
 
Brian Fofian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it to work! You're the man Rick!
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In "real life" I would use a zero-based index and ditch the temporary variables. The result is an embarrassingly simple expression:

static public boolean isZero(int position, int value)
{
return (value &= (1 << position)) == 0;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic