Notes


Find and replace in Vim

To replace all foo with bar:

:%s/foo/bar/g

Automatically regenerate PDF after saving Latex file in Vim

We execute pdflatex [current-file] after we saved the current .tex file in Vim:

autocmd BufWritePost *.tex silent! execute "!pdflatex % >/dev/null 2>&1" | redraw!

Notice that it outputs files to the current working directory.

Count number of 1 bits

A brute-force approach is to simply count the number of 1’s in the binary number:

function countOneBits(num) {
  // Convert number to a base-2 string
  const binaryString = num.toString(2)

  // Remove all 0's so it returns the number of 1's
  return binaryString.replace(/0/g, '').length
}

A bit-manipulation approach is to count how many times we eliminate 1’s in the binary number:

function countOneBits(num) {
  let count = 0

  while (num !== 0) {
    // Eliminate the least significant 1
    num = num & (num - 1)
    count++
  }

  return count
}

Edit