Number representation

Port numbers

In the TCP and UDP protocols, at layer four of the stack, there is a 16-bit binary field known as a port. This is treated as a strictly positive integer.

Octal representation

One octal corresponds to three bits. The only use of octal in modern day computing is in UNIX file permission systems. As UNIX is written in C, it has a way to define integers in octal. This could be used to manipulate files from code.

unsigned int uix = 010; // The leading ZERO means that this is an octal number. This actually equals 8 in decimal

printf("uix: %o", uix);

The filesystem in UNIX is more complicated than this though. Each file has 3 extra bits that allow for special functionality:

  • setuid bit - allows executable to run as another user. This is a security risk as it allows for root user dos.
  • setgid bit - allows executable to change group privileges. This is a security risk also.
  • sticky but - almost obsolete, but it has specialist use cases.

Register operations

Initialise to zero

One option would be to load a literal value of zero from memory. But on some architectures it would be faster to execute R XOR R.

Swap two registers

Register pressure can cause all registers to be used. If R1 and R2 need to be swapped without using a third register or memory, it can be done using the following method.

R1 = b1001
R2 = b1011

R1 = R1 XOR R2 (0010)
R2 = R1 XOR R2 (1001)
R1 = R1 XOR R2 (1011)

Shift and mask

An 8 bit byte can be split into two 4 bit nibbles - char x = b11010011.

If the left and right nibbles are to be extracted to stand-alone char values:

static char const RIGHT_MASK = b00001111;
static char const LEFT_MASK = b11110000;

char const right_nibble = x & RIGHT_MASK;
char const left_nibble = x & LEFT_MASK;

A mask and shift can be defined to extract any number of bits from any position in any length of bit sequence. This is heavily used in embedded systems and in implementing data communication stacks.

IPv4 network and host addresses - CIDR

Each NIC on the Internet has a globally unique 32 bit address. This splits into:

  • Network address - left hand bits
  • Host - remaining right hand bits

When routing packets over the Internet, routers only need to worry about the network address part. The modern day approach to this is Classless Inter Domain Routing. With this, a definition of where to split the 32-bits is needed. This is called a Network Mask.