Tian Zhang

Greenhorn

Posts: 26

posted 12 years ago

hello,

i have the following problem:

consider implementing the following properties of the class fruit which has the following properties:

fruit.isTasty, fruit.isJuicy, fruit.hasPeel

i want to store these boolean properties in a single byte. i would presume that one goes about doing this by delcaring the following constants:

TASTY = 2^0;

JUICY = 2^1;

PEEL = 2^3;

and then a bitmap:

byte properties = isTasty + isJuicy + hasPeel;

the only problem is that my bitwise operators skills are a bit rusty. could someone "optimise"/correct the following code?

public boolean isTasty(){

return properties & this.TASTY;

}

public boolean setTasty(boolean value){

this.isTasty = this.isTasty | TASY;

}

also do bitwise operators work on longs?

thanks

i have the following problem:

consider implementing the following properties of the class fruit which has the following properties:

fruit.isTasty, fruit.isJuicy, fruit.hasPeel

i want to store these boolean properties in a single byte. i would presume that one goes about doing this by delcaring the following constants:

TASTY = 2^0;

JUICY = 2^1;

PEEL = 2^3;

and then a bitmap:

byte properties = isTasty + isJuicy + hasPeel;

the only problem is that my bitwise operators skills are a bit rusty. could someone "optimise"/correct the following code?

public boolean isTasty(){

return properties & this.TASTY;

}

public boolean setTasty(boolean value){

this.isTasty = this.isTasty | TASY;

}

also do bitwise operators work on longs?

thanks

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

Make sure your bit values are powers of 2 ... 1,2,4,8,16 etc. To set a bit use the or operator. To see if a bit is set use and.

What's unset ... bitmap & ^HAPPY?

See if that gets you going.

What's unset ... bitmap & ^HAPPY?

See if that gets you going.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

Let's look at your constants:

Do you know what is going on here? The ^ operator performs a bitwise exclusive or (XOR). The truth table for this operation is as follows:

In my logic class, my professor calls this the "soup or salad" OR. This is because it is true when only one of the operands is true. Typically when a waiter asks if you want soup or salad, you don't reply "both" like you could if it were an inclusive or.

Anyway, let's analyze the values given to each of your constants:

For simplicity, I will only use 4-bit numbers since I don't want to write out all those leading zeros in a real 16-bit int. So this operation looks like this:

And the rest of them are

Is this what you intend? If so, then you probably need to rethink what values will work for each of these constants. Typically when you are using bit masks like this, you should have at most a single bit turned on. The value for JUICY looks a little suspicious to me.

So what values DO you want to use for each bit mask? (I think someone has already given a suggestion, so I won't repeat it.)

Hopefully this helps gives you some ideas about where to go from here. Let us know if you need more help.

Layne

Do you know what is going on here? The ^ operator performs a bitwise exclusive or (XOR). The truth table for this operation is as follows:

In my logic class, my professor calls this the "soup or salad" OR. This is because it is true when only one of the operands is true. Typically when a waiter asks if you want soup or salad, you don't reply "both" like you could if it were an inclusive or.

Anyway, let's analyze the values given to each of your constants:

For simplicity, I will only use 4-bit numbers since I don't want to write out all those leading zeros in a real 16-bit int. So this operation looks like this:

And the rest of them are

Is this what you intend? If so, then you probably need to rethink what values will work for each of these constants. Typically when you are using bit masks like this, you should have at most a single bit turned on. The value for JUICY looks a little suspicious to me.

So what values DO you want to use for each bit mask? (I think someone has already given a suggestion, so I won't repeat it.)

Hopefully this helps gives you some ideas about where to go from here. Let us know if you need more help.

Layne