37
1
Given a number determine if it is a folding number.
A folding number is a number such that if you take it binary representation and "fold" it in half, That is take the result of XNOR multiplication of the first half of the number and the second half with it digits in reverse, you will get zero.
If the number has a odd number of digits in binary its middle digit must be 1 and is ignored when folding.
Since that might be a bit confusing I will give some examples:
178
The binary representation of 178 is
10110010
To fold this we first split it in half
1011 0010
We reverse the second half
1011
0100
And we XNOR the two halves:
0000
This is zero so this is a folding number.
1644
The binary representation of 1644 is
11001101100
To fold this we first split it in half
11001 1 01100
The middle bit is 1 so we throw it out.
11001 01100
We reverse the second half
11001
00110
And we XNOR the two halves:
00000
This is zero so this is a folding number.
4254
The binary representation of 4254 is
1000010011110
To fold this we first split it in half
100001 0 011110
The middle bit is 0 so this is not a folding number.
Task
Your task is to take in a positive number and return a truthy if the number is folding and falsy if it is not. This is code golf so try to keep the byte count down.
Test Cases
Here are the first 99 folding numbers:
[1, 2, 6, 10, 12, 22, 28, 38, 42, 52, 56, 78, 90, 108, 120, 142, 150, 170, 178, 204, 212, 232, 240, 286, 310, 346, 370, 412, 436, 472, 496, 542, 558, 598, 614, 666, 682, 722, 738, 796, 812, 852, 868, 920, 936, 976, 992, 1086, 1134, 1206, 1254, 1338, 1386, 1458, 1506, 1596, 1644, 1716, 1764, 1848, 1896, 1968, 2016, 2110, 2142, 2222, 2254, 2358, 2390, 2470, 2502, 2618, 2650, 2730, 2762, 2866, 2898, 2978, 3010, 3132, 3164, 3244, 3276, 3380, 3412, 3492, 3524, 3640, 3672, 3752, 3784, 3888, 3920, 4000, 4032, 4222, 4318, 4462, 4558]
Is 4 not a folding number? – Adnan – 2016-10-06T13:23:59.797
1@Adnan The middle bit is
0
, so no. (It might be worth having a third worked example like this though.) Same goes for 18. – Martin Ender – 2016-10-06T13:24:18.740@MartinEnder Ahh, I missed that part. Thanks :) – Adnan – 2016-10-06T13:25:21.680
@Adnan Whoops. Mobile :-) – Luis Mendo – 2016-10-06T13:37:05.170
1why does the middle number have to be one (in odd digit binary #s)? was that arbitrary or was there a reason? – greyShift – 2016-10-06T14:41:23.277
3@timrxd if you try to fold a number by adding up opposite digits, a number with a one in the center you will get a string of all ones. If it has a zero in the center you will end with a zero in the result. – Post Rock Garf Hunter – 2016-10-06T14:45:01.120
I know you gave one example of non-folding but if all your test cases are folding numbers then
return true
would pass. You should have at least three negative test cases, one with 0 in the middle, one with 1 in the middle, and one with an even number of bits. – David Conrad – 2016-10-07T07:02:56.443Isn't bitwise XNOR just AND? – Cedric Reichenbach – 2016-10-07T13:51:52.800
@CedricReichenbach No, it's not. XNOR is the NOT of the XOR. It is pretty much an AND except 0 XNOR 0 returns 1. – ericw31415 – 2016-10-07T14:56:47.630