Skip to main content
2 of 4
added Port of fr0DDY´s
Titus
  • 14.8k
  • 1
  • 24
  • 41

#PHP, not competing

I wanted to do it without using strings at all.

iterative solution, 78 bytes

for($x=log($n=$argv[1],2);$i<$x&($n>>$i^$n>>$x-$i^1);$i++);echo$i<$x/2?NO:YES;

recursive solution, 113 bytes

function p($n,$x=0){return$n<2?$n:is_pal(($n&(1<<$x=log($n,2)/2)-1)^$n>>$x+!is_int($x));}echo p($argv[1])?YES:NO;

If n is a binary palindrome, the upper half xor the lower half is also a binary palindrome and vice versa.


a port of the excellent C answer from fR0DDY, 58 bytes

for($x=2*$v=$argv[1];$x/=2;$r=$r*2|$x&1);echo$r-$v?NO:YES;

a binary reverse. Columbus´ egg.

Titus
  • 14.8k
  • 1
  • 24
  • 41