Bens Stuff

Tuesday, August 31, 2004

More of them shifty bits

This Javaranch article is a decent tutorial on bit shifting. I do have a question for some people that have used this stuff. Lets say I have a ten character field that can be either Y or N. I suppose you could think of them as 1 and 0 as well. Anyways, what would be the best way to read these into a set of 10 boolean values? Right now I just use the following simple code:

myObject.setTrueFalseFieldn( myString.charAt(n)=='Y' );

and I repeat it 10 times. Any ideas on how to improve that code? Geez, you'd think I'm some kid straight out of college with this simple of a question...

Another post I had dealing with bits.

4 Comments:

  • As long as you have fields like this:

    myObject.setTrueFalseField1
    myObject.setTrueFalseField2
    myObject.setTrueFalseField3

    you won't be able to do much to improve it.


    for(int x = 0; x < 9; x++) {
    myObject.setTrueFalseField(x,
    myString.charAt(n)=='Y');
    }


    Would be better if you could change myObject. Blogger is very lame and I'm sure it will muck up my code because they don't allow the pre html tag. god knows allowing the pre html tag in a comment would cause all hell to break loose.

    By Anonymous Anonymous, at 5:46 PM  

  • Well,there are several ways of doing that.
    1- Use BitSet class. it carries a long value ant has bit operation methods in it
    2- Create your own attribute class, and extend, or use it directly within the class you want to have multiple attributes. This class can have one integer in it and at least three basic bit operations.
    class Atrribute
    {
    int attributes = 0;
    public void setAttr(int mask)
    {
    attributes |= mask;
    }
    public void resetAttr(int mask)
    {
    attributes &=(~mask)
    }
    public boolean isAttrSet(int mask)
    {
    if(attributes&mask == 0)
    return false;
    return true;
    }
    }

    Then you should define the mask values as this:
    lets say you have a human object,

    public class Human extends Attribute
    {
    public static final ATTR_BLONDE = 0x00000001;
    public static final ATTR_TALL = 0x00000002;
    public static final ATTR_STUPID = 0x00000004;
    public static final ATTR_HARDWORKING= 0x00000008;

    }

    Human human = new Human();
    human.setAttr(Human.ATTR_BLONDE|Human.ATTR_STUPID);

    if(human.isAttrSet(Human.ATTR_HARDWORKING))
    {
    Blah..
    }

    so on.. of course, write unit tests, because this class may become tricky. Also you dont have to extend, you can use a Attribute object within the Human class. Also, prefer to use an integer value in the Attribute class..

    There are other ways, i do not say this is te best method. However, it is memory efficent, and fast..

    By Anonymous Anonymous, at 9:25 PM  

  • I'm beginning to think I should just leave it alone. No need to overly complicate that code. It works, it's not pretty, but it works.
    The real reason I was trying to change it was so I could have some experience with bit manipulation. Since I'll be taking the SCJP(or is it SJCP) in a couple weeks, I figure I should brush up on some of that stuff.

    By Blogger BenC, at 9:53 AM  

  • i misunderstand your question first, you can use it like this:

    lets say you want to convert a string "YNYYYNYYNYNNY" to an integer number and bits representing the points, the easiest way is actually close to what you do.


    int result=0;
    // check for integer limit before
    if(myString.length()<32)
    for(int i = myString.length()-1 ; i>=0 ; i--)
    {
    int j = myString.charAt(i)=='Y' ? 1 : 0;
    result = (result | j)<<1;
    }

    there you go. for example is the string is "YNNNYYNN"
    you will have the equivalent integer of binary number "10001100".

    By Anonymous Anonymous, at 1:41 PM  

Post a Comment

<< Home