0

I have found this if condition using bitwise, I don't know how to convert it to work with normal logical operator.

if (out[currentState] & (1 << j))

How to provide an equivalent condition that doesn't use bitwise?

phuclv
  • 37,963
  • 15
  • 156
  • 475
v_head
  • 759
  • 4
  • 13
  • 5
    You can’t - at least not efficiently. This is testing a single bit of the data, which is best done with bitwise operators. – nneonneo Feb 19 '20 at 04:01
  • 1
    This is checking if the bit on position "j" is set. Why shouldn't it be a bitwise operation? – JBernardo Feb 19 '20 at 04:02
  • What is the range of `j` and type of `out`? Post a [mcve] – chux - Reinstate Monica Feb 19 '20 at 04:02
  • even not efficiently, how would you provide an equivalence? – v_head Feb 19 '20 at 04:02
  • @Reinstate, http://cpp.sh/33squ, line 127 – v_head Feb 19 '20 at 04:04
  • 2
    The proper way to do this is with a bitwise operation. Why would you need an equivalent that doesn't work as well, unless it's for a homework assignment? – Ken White Feb 19 '20 at 04:04
  • 1
    What's wrong with bitwise operations? They much faster and easier to master, so go for it – ark1974 Feb 19 '20 at 04:04
  • @KenWhite right – v_head Feb 19 '20 at 04:05
  • 1
    @KenWhite This is not true. [1](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions), [2](https://meta.stackexchange.com/questions/18242/what-is-the-policy-here-on-homework). However, OP should make the best effort, which is what we require from all askers. – Amadan Feb 19 '20 at 04:08
  • 1
    @Amadan: From the [help]: *Questions asking for homework help **must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it**.* None of that is contained in this question in its current state. – Ken White Feb 19 '20 at 04:09
  • Yes, I agree with that. I do not agree with the blanket "We don't do homework" statement, unless you meant it as "We will not do your homework for you without you doing anything" (but this is also true of a non-homework problem). But it can be interpreted as "We will not help with homework whatsoever". – Amadan Feb 19 '20 at 04:11
  • @Amadan, Kenwhite, this was just basic syntax question. – v_head Feb 19 '20 at 04:20
  • Bitwise operations are not less "normal" than logical ones, they just have different purpose. So I vote to close your question as pointless. – Slava Feb 19 '20 at 15:12

2 Answers2

2

Bit shifts are basically multiplications and divisions by 2. The above condition is equivalent to

if ((out[currentState] >> j) & 1)

which can be converted to

if ((int)(out[currentState] / pow(2, j)) % 2)
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • @user12614580 how is that related to this question? – phuclv Feb 19 '20 at 04:19
  • I just want to know their equivalences too? 1- if (out[currentState] |= (1 << i)); 2- if (out[g[state][ch]] |= out[failure]); – v_head Feb 19 '20 at 04:32
  • yes. It can be easily converted to normal math. Setting a bit to 1 is just like setting it to 0 then set it to 1 which is a `+` operation. Or add directly with the remainder modulo 2 – phuclv Feb 19 '20 at 04:45
  • I haven't fully got you, can you provide both of these ways, it's interesting – v_head Feb 19 '20 at 04:48
  • 1
    it's out of scope for this question. Just think about it yourself, I've already given all the hints. See also [How can bitwise operations be modeled with math?](https://math.stackexchange.com/q/2305727/90333), [bitwise operations - Mathematical equivalents](https://en.wikipedia.org/wiki/Bitwise_operation#Mathematical_equivalents), [mathematical equation for AND bitwise operation?](https://stackoverflow.com/q/7199625/995714) – phuclv Feb 19 '20 at 04:54
  • "It can be easily converted to normal math." question is should it? Your second statement is much less readable that the first and original. I do not criticize your answer, just want to point that it is not clear what the purpose of the action asked by OP. – Slava Feb 19 '20 at 15:17
-1

Bitwise operations aren't that difficult to learn, in fact they're much faster than other data type operations. However, you can use a function, to hide the complicated computation, like so:

 bool Check( const Foo& foo, int pos) {
     return foo & 1 << pos;

if(Check(out[currentState], j)) dosomething();
ark1974
  • 615
  • 5
  • 16