implement isPalindrome(int n)
1,568.1K Views
Implement the function boolean isPalindrome (int n); Which will return true if the bit-wise representation of the integer is a palindrome and false otherwise.
bool
isNthBitSet(unsigned
int
x, unsigned
int
n)
{
return
(x & (1 << (n-1)))?
true
:
false
;
}
bool
isPalindrome(unsigned
int
x)
{
int
l = 1;
// Initialize left position
int
r =
sizeof
(unsigned
int
)*8;
// initialize right position
// One by one compare bits
while
(l < r)
{
if
(isNthBitSet(x, l) != isNthBitSet(x, r))
return
false
;
l++;
r--;
}
return
true
;
}
Your Answer
More puzzles to try-
What am I?
A natural state, I’m sought by all. Go without me, and you shall fall. You do me when you spend, ...Read More »Logical Berries
There is a jar in which there are two types of candies. 20 blueberries and 16 strawberries. You perform the ...Read More »Cut cube into small cubes
A solid, four-inch cube of wood is coated with blue paint on all six sides. Then the cube is cut ...Read More »Color of the hat puzzle
Four men walk into the desert. Suddenly all four are simultaneously knocked out. They awake buried to their heads in ...Read More »How did she die?
A woman is seated and is writing. There is a thunderstorm outside and she dies as a consequence. How did ...Read More »Is the square in the middle a perfect square?
Is the square in the middle a perfect square? Although it is a perfect square, the sides may appear to ...Read More »Closed Area riddle
If, 9999 = 4, 8888 = 8, 1816 = 6, 1212 = 0, then 1919 = ?Read More »The Monkey and the Coconut
Ten people land on a deserted island. There they find lots of coconuts and a monkey. During their first day ...Read More »Good for health bad for waist
My family is known to be crazy; I am good when I am alone and when spread I am amazing; ...Read More »What is the code ?
A man desired to get into his work building, however he had forgotten his code. However, he did recollect five ...Read More »Snake on the Road
In a jungle where there are no street lights or any other artificial source of lights, I notice a black ...Read More »Puzzle fry Derby
After the local Puzzle fry Derby, some friends were telling everyone who had won. Unfortunately, the friends were having problems ...Read More »What does the man smuggle?
The king of Persia has a visitor who will be his new minister of defence.When he comes the king greets ...Read More »Tell The Thoughts
Here’s a Riddle Me This puzzle without the riddle! Solve the puzzle to discover Katherine Hepburn’s thoughts on acting. Answer ...Read More »How old are kids
Two old friends, Jack and Bill, meet after a long time. Jack: Hey, how are you man? Bill: Not bad, got married ...Read More »He ___ up from his seat to give her a ___
What is it?Read More »Can you find the killer?
Sherlock holmes is called upon on a crime scene. A woman has been killed and there are only three suspects ...Read More »Hidden Picture Riddle
Can you find a rabbit in the given picture?Read More »Word can be read the same upside down
What five letter word typed in all capital letters can be read the same upside down ?Read More »Lighter than the feather riddle
What is lighter than a feather but the world’s strongest man can’t hold for Long?Read More »
Accenture Interview PuzzlesAdobe Interview PuzzlesAge RiddleAkbar Birbal PuzzlesAlgorithm PuzzlesAlphabet riddleAmazon Interview PuzzlesAnalytical MathematicsAptitude PuzzleBank PuzzlesBetting PuzzlesBrain TeasersCalendar PuzzlesCards PuzzlesChess Board PuzzleChess PuzzlesChristmas Puzzlecipher PuzzleCivil Services PuzzleClock time puzzleCognizant Interview PuzzlesCoins PuzzleComputational PuzzleconundrumCoronavirus PuzzleCoupondunia Interview PuzzleCritical Thinking Puzzledata analyticsData Structure Interview QuestionsDecode PuzzleDetective PuzzlesDice PuzzleDictionary Riddlesdifficult riddleEasy Math puzzlesEasy puzzlesEinstein puzzleEnglish RiddleFamily Tree Puzzlefamous puzzleFill in the blanks riddlesFinding Killer RiddlesFlipkart interview puzzlesFunny RiddlesGeneral KnowledgeGeographical PuzzleGoogle Code Jam 2014Google Interview PuzzlesGRE PuzzleHard Puzzle