programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# int array length 2, return true if it does not contain a 2 or 3 challenge

sai rama krishna
Ranch Hand
Posts: 536
1
Hi,

I am working on challenge
"
Given an int array length 2, return true if it does not contain a 2 or 3."

i got below 2 approaches. Approach 1 and Approach 2. Both worked fine.

i have not understood meaning of below lines

return (nums[0]&~1)!=2&&(nums[1]|1)!=3//~ bit wise inversion i guess
and

(i|1)==3

Please advise how above approaches worked. I have other simple approaches which also worked fine.

Knute Snortum
Sheriff
Posts: 4274
127
Let's look at if ( (i | 1) == 3)

The "|" operator "ors" bits.

So both 2 and 3 ored with 1 equals 3.

Campbell Ritchie
Marshal
Posts: 56529
172
It might “work” but it is still horrible code.

sai rama krishna
Ranch Hand
Posts: 536
1
if( (i|1)==3 ){ return false; }
above bitwise OR is clear.

I still could not follow bitwise AND solution as below

return (nums[0]&~1)!=2&&(nums[1]|1)!=3;

2 0010
~1 1110 AND
-------------------
2 0010

3 0011
~1 1110 AND
----------------
2 0010

nums[1]|1
2 0010
1 0001 OR
-----------
3 0011

sai rama krishna
Ranch Hand
Posts: 536
1
It might “work” but it is still horrible code.

i agree. Just trying to learn different approaches.

Knute Snortum
Sheriff
Posts: 4274
127

This doesn't make much sense since it's testing the first and second elements. You could have one element or 20.

Campbell Ritchie
Marshal
Posts: 56529
172
It did say in the thread title the array contains 2 elements.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Those are all interesting and clever tricks, but they are only useful for learning how to use bitwise operators.

In real code you should never use these tricks, because it makes the code hard to understand for other programmers or even for yourself if you have to work with it later. Code that's hard to understand is more prone to be a source of bugs.

Sometimes people think that with these kinds of clever tricks their code will be faster or more efficient, but in reality these things almost never matter for the real performance of your whole program. So, don't write code in this way because you think it makes your program faster.

Campbell Ritchie
Marshal
Posts: 56529
172
If you use the commoner operators you can easily change the code to whether the array contains 3 or 4. That would not be possible with the bitwise or operator. Of course you can do this:-And if you can't see that code is even worse than what you had …

If you are going to use code suitable only for party tricks, I have the following challenge. Complete this methodAdditional rules. You must replace my ellipsis ... with “real” code. You must not use the semicolon key, nor any line‑end keys (e.g. enter) in that code. So you are only allowed one line. You must use the bitwise or operator.