CS 202 - Notes 2016-03-04

Bitwise operators

Basic Boolean operations: AND, OR, NOT, (and XOR)

Reminder: & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), and ^ (bitwise XOR).

We added two more bitwise operators: » (right shift) and « (left shift)

These shift the bits of the number over by some specified amount

Example: 10 » 1 = 5

In binary: 1010 » 1 = 101

Note that this is the equivalent to dividing the number by 2. Correspondingly, left shifting multiplies the number by two (just as similar shifts would divide and multiply by 10 in decimal).

See 09-bits.c for these operations in use.

Flags

A frequent use for bitwise operations is creating “flag values”. These are integers that are treated as essentially an array of bits, with each individual bit representing a different value. They are compact and it makes it easy to check or set multiple Boolean values simultaneously. Again, see 09-bits.c for an example of this.

We can see something similar if we look at file permisions on *nix machines. here is an example of listing the contents of a directory and looking at the permissions:

[candrews@abe src_inclass]$ ls -l
total 116
-rwxrwxr-x 1 candrews candrews 8560 Feb 15 10:58 01-first
-rw-rw-r-- 1 candrews candrews  189 Feb 17 10:43 01-first.c
-rw-rw-r-- 1 candrews candrews  511 Feb 15 10:36 01-first.s
-rwxrwxr-x 1 candrews candrews 8672 Feb 17 10:42 02-power
-rw-rw-r-- 1 candrews candrews  555 Feb 17 10:42 02-power.c
-rwxrwxr-x 1 candrews candrews 8672 Feb 17 11:02 03-swap
-rw-rw-r-- 1 candrews candrews  665 Feb 17 11:02 03-swap.c
-rwxrwxr-x 1 candrews candrews 8560 Feb 19 10:59 04-arrays
-rw-rw-r-- 1 candrews candrews 3514 Feb 19 12:59 04-arrays.c
-rwxrwxr-x 1 candrews candrews 8696 Feb 22 10:26 05-maxarray
-rw-rw-r-- 1 candrews candrews  758 Feb 22 10:26 05-maxarray.c
-rwxrwxr-x 1 candrews candrews 8616 Feb 22 11:03 06-strings
-rw-rw-r-- 1 candrews candrews 1063 Feb 22 11:03 06-strings.c
-rw-rw---- 1 candrews candrews  450 Mar  4 10:41 09-bits.c
-rwxrwxr-x 1 candrews candrews 8608 Mar  4 10:28 bits

The columns on the left give us the permissions set for the file: r - read, w - write, x - execute. For each file, we have three sets of these, the first three are for the owner, the second is for members of the owner’s group, and the last is for everyone. Each one of these flags can be set to on or off, so it is easy to see how this could be stored as individual bits.

The chmod tool, which we use to alter the permissions, can take advantage of this, allowing us to set the permissions using an octal number (we like octal for this because each digit of octal corresponds to 3 of binary). Once we are comfortable with binary-octal conversions, this makes it easier set the permissions because we can specify the state of all of the flags at once.

Examples: 755 - rwxr-x-r-x (I can read, write, and execute, everyone else and read and execute, but they can’t edit) 600 - rw——- (I can read and write, and no one else can)

Floating point

All of the numbers we have looked at so far are integers. What about reals?

The radix point works the same in binary as it does in decimal

d1d0d-1d-2 = d121 + d020 + d-12-2 + d-22-2

Example: 110.11 = 4 + 2 + 0 + .5 + .25 = 6.75

Our problem is that we can’t represent the radix point in the computer – still only have bits.

So, we come up with a new storage technique based on scientific notation.

Example: 110.11 = 1.1011 x 22

More generally, F x 2E. So, we take the available bits and divide them up into room for the fractional part F and the exponent E.

We would like signed values, so we will also add in a sign bit s, giving us -1S x F x 2E

As a part of our notation, we need to normalize the number so that it only has one digit to the left of the radix point. Note that in binary, the most significant digit is always 1, so our normalized number will always start with “1.”. Since we know this is always the case, we won’t both storing it.

So, now our number looks like -1S x 1.M x 2E, where M is the mantissa. So, in the computer we need 1 bit for S, some bits for E, and whatever is left over for M.

Excess notation

The next issue is that we would like negative exponents, so however we store E has to be signed. We could use something like two’s compliment, but we are going to use something else called excess notation, which has the advantage that small numbers will look small and large numbers will look big.

The idea is that we subtract some fixed number (called the bias) from the stored value. To make the number of positive and negative numbers roughly the same, we want to split the available representations, which we can do by using a bias of 2N-1 or 2N-1 -1 (the choices comes down to whether or not we want one extra positive or one extra negative).

Consider a three bit number. Our bias could either be 3 or 4:

Binary Stored value Excess-4 interpretation Excess-3 interpretation
000 0 -4 -3
001 1 -3 -2
010 2 -2 -1
011 3 -1 0
100 4 0 1
101 5 1 2
110 6 2 3
111 7 3 4