#### Archived

This topic is now archived and is closed to further replies.

# Bits, setting without a compare

This topic is 5401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is the best I can come up with for setting a bit : / int byte = index / 8; int bit = index % 8; ((bits_array[byte] & (1 << bit)) && !value) ? bits_array[byte] &= (~(1 << bit)) : (bits_array[byte] |= (value << bit)); I''m unsure whether to use !(~(bits_array[byte]) & (1 << bit)); or ((bits_array[byte]) & (1 << bit)) ? 1 : 0; for getting bits. The latter''s more intuitive. Are there any better/more obvious ways to do this? I''ve searched for previous posts and came up with similar (but not exactly the same) issues. Thanks, Ro_Akira

##### Share on other sites
Setting bits is easy. Your answer is so confusing that I think it will take some context before anyone here can be of any help. What are you trying to do?

Bit sets are just

variable |= (1<<bit);

Why would you need to compare? And, what is bits_array?

##### Share on other sites
Looking again, it looks like you might want to xor.

##### Share on other sites
Yeah, it is confusing.

variable |= (1 << bit);
works fine.

variable |= (value << bit);
as I''m trying to do, only works if value is 1. If it''s 0,
the OR has about as much effect as an "ineffectual thing". I hope I''m right about that.

You asked what bits_array is. Well, time to regret it!...

bits_array is just int bits_array["bytes needed to hold the number of bits I need"]. i.e. If I need 8 bits, I''ll need 1 byte, If I need 9 bits, I''ll need 2 bytes, etc.

"index" is an index into the array, in bits.
e.g. bits_array[9] should access the first bit, in the second byte, of the array.

Right, take the first line:

((bits_array[byte] & (1 << bit)) && !value)
This says "If the bit I''m trying to set, is set in the array, and the "value" I''m trying to set it to is 0, then do this:

bits_array[byte] &= (~(1 << bit));
(Set it to zero)

otherwise, do this:

(bits_array[byte] |= (value << bit));
(Set it to one)

These other two lines:

!(~(bits_array[byte]) & (1 << bit));
and
((bits_array[byte]) & (1 << bit)) ? 1 : 0;

are unrelated. Their more in the getting bits business.

Thanks,
Ro_Akira

##### Share on other sites
Got it:

byte = ((byte&~(1<<bit))|(value<<bit));

With the appropriate variables subbed in.

##### Share on other sites
That worked!!! One would have thought such a thing would be in any document on bits.

Did you just come up with that now? Or did you put my searching efforts to shame and find it?!

[EDIT]
Just noticed now, it's an amalgamation of the two branches of the if statement I had. So close! Doh!
[\EDIT]

Looking at it now, I crippled my efforts by assuming I could use x &= y / x|= y or some such as opposed to x = x & y / x = x | y, or the like. It doesn't seem like the code you've written can use those shorter forms.

Any opinion on

!(~(bits_array[byte]) & (1 << bit));
vs
((bits_array[byte]) & (1 << bit)) ? 1 : 0;
?

Cheers,
Ro_Akira

[edited by - Ro_Akira on September 3, 2003 12:43:36 AM]

##### Share on other sites
Do you have a 386 where you don''t have enought Memmory?... sounds or something like that?

##### Share on other sites
More like I''m storing a PVS for terrain, and this, along with another trick, means a 512x512 heightfield (using 16x16 vertex patches) needs only 380k for it instead of 8MB.

The machine it''s running on has 512MB of memory. 1024x1024 heightfields show an even bigger saving. (<2MB vs. 128MB)

##### Share on other sites
quote:
Original post by Ro_Akira
That worked!!! One would have thought such a thing would be in any document on bits.

Did you just come up with that now? Or did you put my searching efforts to shame and find it?!

[EDIT]
Just noticed now, it's an amalgamation of the two branches of the if statement I had. So close! Doh!
[\EDIT]

Looking at it now, I crippled my efforts by assuming I could use x &= y / x|= y or some such as opposed to x = x & y / x = x | y, or the like. It doesn't seem like the code you've written can use those shorter forms.

Any opinion on

!(~(bits_array[byte]) & (1 << bit));
vs
((bits_array[byte]) & (1 << bit)) ? 1 : 0;
?

Cheers,
Ro_Akira

[edited by - Ro_Akira on September 3, 2003 12:43:36 AM]

I don't get what you're trying to do... but, why not simply:

(bits_array[byte]>>bit)&1;

This is much more efficient I think... 1 SHIFT, 1 AND... just re-ordered so you don't have to use a compare/jump, and it still returns a 1,0 .

--- Edit ---
Crap, it screwed up my code a bit.. added some code tags

[edited by - Ready4Dis on September 4, 2003 7:24:42 PM]

##### Share on other sites
Ugh! More of my attempts at code put to shame! That''s what I get for coding (or trying to) at 4 in the morning, I guess.

That works fine Ready4Dis. As you can see, I resorted to adding operations until I got the right result (that''s where the ! and the ~ came from :/ ).

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631358
• Total Posts
2999531
×