Thursday, April 23, 2009

Flipping burgers and bits is not same

How do we flip bits in a integer from p1 to p2 locations in C? Here is the macro for it.
#define flip_bits(x, p1, p2) (x ^ (((unsigned int)~0 >> ((sizeof(int) * 8) - (p2-p1+1))) << p1))

Sunday, April 19, 2009

Traceroute - How it works?

Traceroute is a Linux/Unix utility that is used to find all the routers along the destination host. It works by sending 3 UDP datagrams with destination ports numbers from 33434 to 33534 at a time
with TTL value starting with 1 till the destination is reached. Each router along the way send back ICMP message when it sees TTL value = 1.
A similar utility used in Windows, tracert does not use UDP packets but ICMP echo request (type 8) instead as in the case of ping.

Wednesday, April 1, 2009

Closures

Closures achieve lexical scoping.