• Advertisement

Archived

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

Bits, setting without a compare

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Looking again, it looks like you might want to xor.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Got it:

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

With the appropriate variables subbed in.

Share this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 :/ ).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

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

goes down to:

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

it should be pretty clear how it works from that.

Share this post


Link to post
Share on other sites
quote:
Original post by Ro_Akira
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 :/ ).


Heh, yes... I was up until 3:30am last night hacking away at my program. Glad to be of service, and now if I ever have to implement that in my game engine, I don''t have to think about it a second time . I don''t beleive I could benefit from using bits for anything though, but you never know .

Share this post


Link to post
Share on other sites

  • Advertisement