Skip to content
/ epi Public

πŸ“˜ Elements of Programming Interviews

Notifications You must be signed in to change notification settings

mzmousa/epi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 

Repository files navigation

EPI, 1 month prep

Hex to binary

0x1111 = 0b 1111 1111 1111 1111

Extract first 4 bytes of a word

void ExtractBytes (uint32_t value) {
  uint32_t byte1 = value >> 24;
  uint32_t byte2 = (value >> 16) & 0xff;
  uint32_t byte3 = (value >> 8) & 0xff;
  uint32_t byte4 = value & 0xff;
  ...
}

Count the # of 1 bits in an int

int CountBits (int x) {
  int num_bits = 0;
  while (x) {
    num_bits += x & 1;
    x >>= 1;
  }
  return num_bits;
}

Useful Bitwise Operations

Set xth bit (0 being rightmost bit)
number |= (1 << x);
0000 |= (1 << 3) => 0000 |= 1000 => 1000

Clear xth bit:
number &= ~(1 << x);
1111 &= ~(1 << 2) => 1111 &= ~(0100) => 1111 &= 1011 => 1011

Toggle xth bit, since 0^1=1 and 1^1=0:
number ^= (1 << x);
1111 ^= (1 << 1) => 1111 ^= 0010 => 1101
0000 ^= (1 << 1) => 0000 ^= 0010 => 0010

Get lowest bit in x that is set to 1
x & ~(x-1)
x = 1000; x-1 = 0111; ~(x-1) = 1000; x & ~(x-1) = 1000
x = 1011101; x-1 = 1011100; ~(x-1) = 0100011; x & ~(x-1) = 0000001
x = 10100; x-1 = 10011; ~(x-1) = 01100; x & ~(x-1) = 00100

Clear lowest bit in x that is set to 1
x & (x-1)
x = 1011101; x-1 = 1011100; x&(x-1) = 1011100;
x = 1010; x-1 = 1001; x&(x-1) = 1000;

  • 5.3
  • 5.11
  • 6.5
  • 6.8
  • 7.7
  • 7.8
  • 8.10
  • 9.3
  • 9.9
  • 10.11
  • 11.5
  • 12.5
  • 12.10
  • 13.4
  • 13.7
  • 14.6
  • 14.9
  • 15.5
  • 15.9
  • 16.4
  • 16.9
  • 17.5
  • 17.7
  • 18.8
  • 19.3
  • 20.9
  • 21.1

About

πŸ“˜ Elements of Programming Interviews

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published