Skip to content

Instantly share code, notes, and snippets.

//Brian Kernighan’s Algorithm:
/*
Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the righmost set bit). So if we subtract a number by 1 and do bitwise & with itself (n & (n-1)), we unset the righmost set bit. If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count.
Beauty of the this solution is number of times it loops is equal to the number of set bits in a given integer.
*/
#include<stdio.h>
/* Function to get no of set bits in binary
representation of passed binary no. */
@twmht
twmht / multiprocessing1.py
Created April 3, 2014 13:35
Multiprocessing
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
urls = [
'http://www.python.org',
'http://www.python.org/about/',
'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
'http://www.python.org/doc/',
'http://www.python.org/download/',
'http://www.python.org/getit/',
@twmht
twmht / pythonbrew.sh
Last active August 29, 2015 13:59
Intsall pythonbrew
sudo apt-get install python-setuptools zlib1g-dev libbz2-dev libreadline-dev libgdbm-dev libssl-dev libsqlite3-dev curl build-essential libxml2-dev libxslt1-dev libreadline5 libreadline6-dev libxml2 tk-dev libexpat1-dev libncursesw5-dev
sudo easy_install pythonbrew
pythonbrew_install
#!/usr/bin/env python2
# coding: utf-8
import os,socket,threading,time
#import traceback
allow_delete = False
local_ip = socket.gethostbyname(socket.gethostname())
local_port = 8888
currdir=os.path.abspath('.')
@twmht
twmht / showgit.sh
Created June 29, 2014 09:15
show git branch in bash shell
# show git branch
function git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
echo "("${ref#refs/heads/}")";
}
PS1="${HOSTNAME}:\w\[\033[1;32m\]\$(git_branch)\[\033[0m\]$";
(function($){
// Settings
var repeat = localStorage.repeat || 0,
shuffle = localStorage.shuffle || 'false',
continous = true,
autoplay = false,
playlist = [];
// Load playlist
for (var i=0; i<playlist.length; i++){
@twmht
twmht / Person.java
Created December 25, 2014 15:24
Gzip
package org.gradle;
import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;
import org.apache.commons.collections.list.GrowthList;
@twmht
twmht / gist:cd4d26455c1a31a9065b
Last active August 29, 2015 14:14
Generate cscope db from android projects
#!/bin/bash
#./genDb.sh `pwd`
#export CSCOPE_EDITOR=`which vim`
CSCOPE_FILE=cscope.out
if [ -n "$1" ]; then
echo "Source code directory: " $1
echo "Create file map : " $CSCOPE_FILE
find $1 -name "*.h" -o -name "*.c" -o -name "*.cpp" -o -name "*.java" -o -name "*.aidl" -o -name "*.mk" > $CSCOPE_FILE
#cscope -bkq -i $CSCOPE_FILE
@twmht
twmht / unlock.sh
Created June 11, 2015 05:13
unlock the device
if [ "$(adb shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')" == false ] ; then
echo "Screen is off. Turning on."
adb shell input keyevent 26 # wakeup
adb shell input keyevent 82 # unlock
echo "OK, should be on now."
else
echo "Screen is already on."
fi
@twmht
twmht / autoload.eclim.vim
Created May 11, 2012 15:32
auto switch between autocomplop and neocomplopcache in eclim
" In function! eclim#ExecuteEclim
....
let g:eclimd_running=1
""" Because the snippet may execute multiple times, I just make the auto-switch between these two plugins execute only one time
if !exists('g:mht_enableThePlugin')
"""all the eclimrc settings will store in eclimrc.vim, not vimrc for management-easy reason
source ~/eclimrc.vim
let g:mht_enableThePlugin = 1