implement isPalindrome(int n)
1,640.7K 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-
Apple on Demand Puzzle
An apple seller is hosting a competition. He offers 1000 apples and 10 boxes to the people who pass by. ...Read More »Compare the 2 spotted patterns below. What have they in common?
Bubble Blows
A baby girl blows 18 bubbles, Pops 6 eats 7 and again, She pops 5 and blows 1. How many ...Read More »Once he could see
It does not have eyes, but once it did see. Once it had thoughts, but now it is white and ...Read More »Small room to Big room lateral thinking puzzle
Jim entered in a small room. Door Closed When the door opened, Jim is in a big room. How that’s ...Read More »Mystery in the Cabin
There is so small cabin inside a deep forest. All the forest burned out to worst possible but the inside ...Read More »Cat & Rat riddle
If 5 cats can catch 5 rats in 5 minutes. How many cats do you need to catch 100 rats ...Read More »Calculate the area of the green region labeled as ‘?’
The below figure consists of a rectangle and the numbers labeled over the top represents the area of that particular ...Read More »Drop from tallest building riddle
if You drop him from the tallest building it is fine, but if you drop him in water he wil ...Read More »Prime number divided by 5 puzzle
Can you find out how many prime numbers that are less than 95 will leave an odd reminder when divided ...Read More »Manholes round riddle
Why are manholes round?Read More »Does this look like a spiral?
Does this look like a spiral? It’s actually a bunch of concentric circles. Use your mouse to trace one circle ...Read More »Twenty Story Building on Fire
One day, a twenty story building was on fire. A man jumped out a window and nothing happened to him. ...Read More »Number Sequence Puzzle
Find the missing number in the sequence. 3 5/3 7/5 11/7 ? 17/11Read More »Hot Handle riddle
What is represented by this BrainBat? HOT HOT HANDLE HANDLERead More »How was he shot?
A man was shot while in his car. The person who shot him was outside of the car. However, all ...Read More »What is it?
The maker doesn’t need it, the owner doesn’t want it, the user doesn’t know he’s using it what is it?Read More »Why did the man thank the bartender?
A man runs into a bar and asks for a glass of water. The bartender pulls out a shotgun and ...Read More »How can this be possible ?
Adam and his wife were living in a rural place. On a certain day, Adam wife fell ill and he ...Read More »i am beautiful
I am pretty beautiful, fluttering in the pale-blue sky. Delicate, fragile on the wing, indeed I am a pretty thing. ...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