• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Bit Manipulation Failing D:

Status
Not open for further replies.

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
bool getBit(byte bits, int index) { return 1 == (bits & intToByte(1 << index)); }
Makes no sense to me.

You sure you did not mean...
Code:
bool getBit(byte bits, int index) {
    return (bits & intToByte(1 << index)) != 0;
}
In C everything that is not 0 evaluates true. For any index other than 0 that function would return false even if the bit was set using your function. The correct statement shown in my function tests if it is not equal to 0 mimicing how C evaluates logic.

Alternativly this should also work assuming sign bit is ignored when right shifting (>>> in some languages)...
Code:
bool getBit(byte bits, int index) {
    return (intToByte(bits >> index) & 1) == 1;
}

//does not work
void setBit(byte bits, int index, bool value) {
if (value) {
bits = bits | intToByte(1 << index);
}
else {
bits = bits & ~intToByte(1 << index);
}
}
Logicly it is fine.

What is confusing me is what on earth the "intToByte" function is... There does not seem to be a way to convert byte to int types in SC2?
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
What is confusing me is what on earth the "intToByte" function is... There does not seem to be a way to convert byte to int types in SC2?

byte intToBit(int i) { return i; }

Logicly it is fine.

Yet it doesn't work D:

In C everything that is not 0 evaluates true. For any index other than 0 that function would return false even if the bit was set using your function. The correct statement shown in my function tests if it is not equal to 0 mimicing how C evaluates logic.

I'm checking 1 bit, so it's value can only be either 0 or 1, so != 0 and == 1 are the same things.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
I'm checking 1 bit, so it's value can only be either 0 or 1, so != 0 and == 1 are the same things.
You even understand what the bit opperators are doing? You are checking a single bit but not at bit 0 position. It might only be a single bit but if it is bit 6 then it has a value of 64 and not 1.

Yet it doesn't work D:
In what way?

byte intToBit(int i) { return i; }
That was not the functon I asked for... However I am guessing you are trying to say it is using some kind of type-casting return bug?
 
Status
Not open for further replies.
Top