30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



How would you reverse bits of an integer in an optimized way suitable for an embedded system?

12


Gopal on November 16, 2009 |Edit | Edit

xor the bits with ff.
example : 0000 0011
1111 1111
------------(XOR)
1111 1100

Gopal (with clear example) on November 16, 2009 |Edit | Edit

xor the bits with ff.
example :

0000 0011
1111 1111
------------(XOR)
1111 1100

Anonymous on November 16, 2009 |Edit | Edit

the question is to reverse the bits i.e. input 11110000 output : 00001111

the solution provided above is a toggle which can be done with operator ~

int toggle (int x) { return ~x;)

hemantajay on November 19, 2009 |Edit | Edit

use lookup table to store reversed bits of every possible combination in a 8-bit field, so your lookup table is O(1) in memory with 256 elements. then:


int input,result;
result=((table[input&0xff]<<24)|(table[(input>>8)&0xff]<<16)|(table[(input>>16)&0xff]<<8)|(table[(input>>24)&0xff]));
return result;

sid on December 22, 2009 |Edit | Edit

its an embedded system.. i dont think keeping a table in memory is a good idea.

Anonymous on January 13, 2010 |Edit | Edit

graphics.stanford.edu/~seander/bithacks.html

isisme on January 27, 2010 |Edit | Edit

unsigned int v; // input bits to be reversed
unsigned int r = v; // r will be reversed bits of v; first get LSB of v
int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end

for (v >>= 1; v; v >>= 1)
{
r <<= 1;
r |= v & 1;
s--;
}
r <<= s; // shift when v's highest bits are zero

Nitin on February 22, 2010 |Edit | Edit


int reverser (int in) {

int out = 0;
int m = 0x8000;
int tmp = 0;

for (int i=0; in!=0; i++) {
tmp = in & m;
in = in<<1;
out = (tmp<<i) + out;
}

return out;
}

Radz on March 03, 2010 |Edit | Edit

If you don't wanna use a table in memory, there is one more way to do this.


void reverse(int x)
{
// assume 32-bit number
m = 0x55555555; // 1-bit swap
x = ((x & m) << 1) | ((x & ~m) >> 1);

m = 0x33333333; // 2-bits swap
x = ((x & m) << 2) | ((x & ~m) >> 2);

m = 0x0f0f0f0f; // 4-bits swap
x = ((x & m) << 4) | ((x & ~m) >> 4);

m = 0x00ff00ff; // 8-bits swap
x = ((x & m) << 8) | ((x & ~m) >> 8);

x = (x << 16) | (x >> 16); // 16-bits swap
}

Source from stanford/bithacks.html

Sanjay on July 05, 2010 |Edit | Edit

Recursive version:

int maskarr[] = { 0x55555555, 0x33333333, 0x0f0f0f0f, 0x00ff00ff };

int RecursiveReverse(int x, int i) {
int m;
int p = 1 << i;

if ( i == 4 ) {
return ( x << p ) | ( x >> p);
}

m = maskarr[ i ];
x = ((x & m) << p) | ((x & ~m) >> p);
return RecursiveReverse(x, ++i);
}

NNS Chowdary on August 08, 2010 |Edit | Edit

Keep in memory that size of int is not known.

Thanks,
NNS Chowdary (+91 959 1234 239)

chetan on May 24, 2010 |Edit | Edit

/*swapping function for byte data type. The logic here is Iam extracting two bits at the same time and swapping them,then oring them and storing them in char variable res
*/

void swap_bits ( int no ) {
char up,lo,res ;
char and = 0x01 ;
for ( int i = 0 ; i < 8 ; i++ ) {
lo = val & and ;
and = shift_bits_lo ( and, 7 - i ) ;
up = val & and ;
and = shift_bits_hi ( and, 7 - i ) ;
for ( int j = 7 - i ; j > 0 ; j++ ) {
up = up >> j ;
lo = lo << j ;
}

res = up | lo ;
}
no = res ;

}

char shift_bits_lo ( char no , int shift) {

while ( shift > 0 )
no = no << shift ;
return no ;
}

char shift_bits_hi ( char no , int shift ) {
while ( shift > 0 )
no = no >> shift ;
return no ;
}

Add a Text Comment | Add Runnable Code
Name:
Comment:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out