Issue #12753 has been updated by Yui NARUSE.


Herwin Quarantainenet wrote:
> I can't say the usage of `bittest?` is directly clear to me. Does it test if resulting integer is not equal to `0`? And would we have to use it this way?
> 
> ```ruby
> if (n & 0b10100000).bittest?
> ```

Like
```ruby
 if n.bittest?(0b10100000)
```

> I think a name like `Integer#binary_and?` (maybe shortened to `#binand?`) would result in cleaner code
> 
> ```ruby
> if n.binary_and?(0b10100000)
> ```
> 
> Of course this would require several other implementations as well, for all other binary operators

There's two AND, bitwise and logical.
Therefore it can be `bit_and?`, but there's no reason  to write logical AND as a method, which can be written with `&&`.

----------------------------------------
Feature #12753: Useful operator to check bit-flag is true or false
https://bugs.ruby-lang.org/issues/12753#change-61731

* Author: Satoshi TAGOMORI
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Ruby's 0 is truthy value. It's useful for many cases, but it's confusing and I made many bugs when I'm writing code to handle binary data, because my thought is almost same with one to write C code in such situation.

```ruby
n = get_integer_value
if n & 0b10100000
  # code for the case when flag is true
else
  # never comes here :(
end
```

IMO it's very useful to have methods for such use-cases, like `#and?` and `#xor?` (`#or?` looks not so useful... I can't imagine the use case of this operator, but it's better to have for consistency).

```ruby
n = get_integer_value
case
when n.and?(0b10000000)
  # negative signed char
when n.and?(0b01110000)
  # large positive
else
  # small positive
end
```




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>