Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview: Flipping Bits

Tags:

pseudocode

I recently saw an interview question asking the following:

Given a 32 bit number, write pseudo code to flip the second last bit

What is the best/easiest way to do this?

like image 573
Pieter van Niekerk Avatar asked Jul 06 '10 10:07

Pieter van Niekerk


2 Answers

#define MASK 0x00000002 

new = old ^ MASK

like image 98
Daniel Băluţă Avatar answered Sep 20 '22 16:09

Daniel Băluţă


I see some answers interpret "last bit" as MSB, others as LSB. Perhaps they're looking for candidates smart enough to pause and ask for clarification before cranking out code. That's very important in real-world work.

like image 20
DarenW Avatar answered Sep 18 '22 16:09

DarenW