1. and_bits() usage

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. smile

new topic     » topic index » view message » categorize

2. Re: and_bits() usage

euphoric said...

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. smile

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 
new topic     » goto parent     » topic index » view message » categorize

3. Re: and_bits() usage

(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.

new topic     » goto parent     » topic index » view message » categorize

4. Re: and_bits() usage

interesting

did'nt know that we could now use binary for numbers

is this in the docs?

new topic     » goto parent     » topic index » view message » categorize

5. Re: and_bits() usage

s'ok

found what i was looking for in manual

new topic     » goto parent     » topic index » view message » categorize

6. Re: and_bits() usage

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.

new topic     » goto parent     » topic index » view message » categorize

7. Re: and_bits() usage

mic_ said...

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.

new topic     » goto parent     » topic index » view message » categorize

8. Re: and_bits() usage

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? smile

new topic     » goto parent     » topic index » view message » categorize

9. Re: and_bits() usage

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

new topic     » goto parent     » topic index » view message » categorize

10. Re: and_bits() usage

euphoric said...

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? smile

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu