• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might “work” but it is still horrible code.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



Please advise
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might “work” but it is still horrible code.

i agree. Just trying to learn different approaches.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It did say in the thread title the array contains 2 elements.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!