🔒 Locked
Bits, setting without a compare
Advertisement
Original Post
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
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
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
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]
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]
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)
The machine it''s running on has 512MB of memory. 1024x1024 heightfields show an even bigger saving. (<2MB vs. 128MB)
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]
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 :/ ).
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 :/ ).
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement