• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Bit Manipulation Failing D:

Status
Not open for further replies.
Code:
//works?? don't know
bool getBit(byte bits, int index) { return 1 == (bits & intToByte(1 << index)); }

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

from http://stackoverflow.com/questions/4674006/set-specific-bit-in-byte
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,259
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?
 
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,259
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