1. and_bits() usage
- Posted by euphoric (admin) Oct 06, 2009
- 1154 views
- Last edited Oct 07, 2009
I have a binary value such as this:
myval = 1010
and want to know if the second bit is set. Am I supposed to do this:
if and_bits( myval, 2 ) = 2 then ... end if
or what?
There's gotta be a better way.
2. Re: and_bits() usage
- Posted by jaygade Oct 07, 2009
- 1127 views
I have a binary value such as this:
myval = 1010
and want to know if the second bit is set. Am I supposed to do this:
if and_bits( myval, 2 ) = 2 then ... end if
or what?
There's gotta be a better way.
Umm, wouldn't it be:
myval = 0b0101 -- assuming 4.0+ if and_bits(myval, 0b10) != 0 then ... end if
In decimal it would be like this:
myval = 5 -- 0101 binary if and_bits(myval, 2) != 0 -- 5 and 2 is equal to 0101 and 0010 is equal to 0 ... end if
3. Re: and_bits() usage
- Posted by jaygade Oct 07, 2009
- 1115 views
(Stupid answer erased)
Edit: still got it wrong.
Crap.
Go back to the previous answer. if and_bits(myval, bit) = 0 then your bit is clear. If and_bits(myval, bit) = bit then your bit is set. "bit" being the actual value of the bit, not its position.
4. Re: and_bits() usage
- Posted by raseu Oct 07, 2009
- 1096 views
interesting
did'nt know that we could now use binary for numbers
is this in the docs?
5. Re: and_bits() usage
- Posted by raseu Oct 07, 2009
- 1091 views
s'ok
found what i was looking for in manual
6. Re: and_bits() usage
- Posted by mic_ Oct 07, 2009
- 1105 views
This is not Java, so
if and_bits( myval, 2 ) then
would be sufficient. I've never looked at what kinds of optimizations the interpreter does though, so it might remove the equality check automatically.
7. Re: and_bits() usage
- Posted by DerekParnell (admin) Oct 07, 2009
- 1117 views
This is not Java, so
if and_bits( myval, 2 ) then
would be sufficient. I've never looked at what kinds of optimizations the interpreter does though, so it might remove the equality check automatically.
Yes, if and_bits( myval, 2 ) then is slightly more efficient than if and_bits( myval, 2 ) != 0 then.
But note that both these are testing if any of the required bits are on. As you are testing for only one bit it doesn't matter in this case, but the test was and_bits(myval, 0b11), you would only get a true result if either bit were on. If you needed to test that both were on you need to code and_its(myval, testbits) = testbits.
8. Re: and_bits() usage
- Posted by euphoric (admin) Oct 07, 2009
- 1110 views
Okay, so for testing ONE or more bits, you can do this:
my_val = 10 test_val = 2 if and_bits( my_val, test_val ) then --... end if
Do we all agree?
9. Re: and_bits() usage
- Posted by ghaberek (admin) Oct 07, 2009
- 1075 views
Here is how I use and_bits().
constant FLAG1 = #0001 constant FLAG2 = #0002 constant FLAG3 = #0004 constant FLAG4 = #0008 atom flags = or_bits( FLAG1, FLAG3 ) -- flags is #0005 if and_bits( flags, FLAG1 ) then -- output is #0001 (true) -- FLAG1 is set end if if and_bits( flags, FLAG2 ) then -- output is #0000 (false) -- FLAG2 is set end if if and_bits( flags, FLAG3 ) then -- output is #0004 (true) -- FLAG3 is set end if if and_bits( flags, FLAG4 ) then -- output is #0000 (false) -- FLAG4 is set end if
-Greg
10. Re: and_bits() usage
- Posted by jemima Oct 12, 2009
- 1052 views
Okay, so for testing ONE or more bits, you can do this:
my_val = 10 test_val = 2 if and_bits( my_val, test_val ) then --... end if
Do we all agree?
no!
The above is true for testing ONE or ANY of the specified bits
(which in your example was just the one bit anyway,
and I suspect you meant ANY when you said "more").
Testing that ALL the specified bits are set is and_bits(flag,mask)=mask
(but as said you don't need the =mask if it is just one bit).
Testing that NONE of the specified bits are set is not and_bits(flag,mask).
HTH