MIPS Bitwise Operators


MIPS includes instructions for performing logical operations such as AND and OR on the bits of registers. Recall that 1 is true and 0 is false. The bitwise operations perform the operation on the corresponding bits of two values and store the result in a register. For example, the instructions to AND and OR two registers will operate as follows:

    Rs:       0000 0000 1110 0000 1100 1010 1111 0011
    Rt:       0000 1100 0111 1111 1010 0110 0001 1111

    Rs · Rt = 0000 0000 0110 0000 1000 0010 0001 0011
    Rs + Rt = 0000 1100 1111 1111 1110 1110 1111 1111

Recall the following Boolean identities

   0 + x = x
   0 · x = 0

   1 + x = 1
   1 · x = x

We can use these identities to set a bit (make its value 1), to reset a bit (make its value 0) or test a bit (check whether its value is 1). One of the operands in a bitwise operation is usually called the mask. The bits of the mask are chosen to select which bits we want to set, reset, or test in the other operand.

When we are discussing the individual bits in an n bit operand, we will call the low order (rightmost) bit "bit 0" and the high order (leftmost) bit "bit n". For the following examples, we use 8 bit operands for simplicity.

Setting a Bit

To set a bit, use OR with a mask that has a 1 in the position which is to be set. All other positions in the mask contain 0. It is also possible to set multiple bits by putting 1's in more than one position of the mask. For example, suppose we have the bit string 0000 0110 and we want to set bit 0 (change it to 1). We can use mask 0000 00012 = 0116 and the operation OR:

    0000 0110 + 0000 0001 = 0000 0111

This sets bit 0 and leaves all other bits unchanged. The mask 0000 10002 = 0816 can be used to set bit 3,and the mask 0110 00002 = 6016 can be used to set bits 5 and 6.

    0000 0110 + 0000 1000 = 0000 1110
    0000 0110 + 0110 0000 = 0110 0110

Resetting a Bit

To reset a bit, use AND with a mask that has a 0 in the position which is to be reset. All other positions in the mask contain 1. It is also possible to reset multiple bits by putting 0's in more than one position of the mask. For example, suppose we have the bit string 1110 0110 and we want to reset bit 2 (change it to 0). We can use mask 1111 10112 = FB16 and the operation AND:

    1110 0110 · 1111 1011 = 1110 0010

This resets bit 2 and leaves all other bits unchanged. The mask 1111 11012 = FD16 can be used to reset bit 1,and the mask 1001 11112 = 9F16 can be used to reset bits 5 and 6.

    1110 0110 · 1111 1101 = 1110 0100
    1110 0110 · 1001 1111 = 1000 0110

Testing a Bit

To test a bit, use AND with a mask that has a 1 in the position which is to be tested. All other positions in the mask contain 0. For example, suppose we have the bit string 1110 0110 and we want to test bit 5 (check its value). We can use mask 0010 00002 = 2016 and the operation AND:

    1110 0110 · 0010 0000 = 0010 0000

Since bit 5 was set, this results in a nonzero value. However, suppose we have the bit string 1100 0110 and we AND with the same mask:

    1100 0110 · 0010 0000 = 0000 0000

In this case bit 5 was not set, and it results in a zero value. So after the AND we need to test the result, if the result is nonzero, then the bit is set, otherwise it is not.

MIPS Bitwise Instructions

MIPS has two types of bitwise instructions: those that operate on registers, and those that operate on a register and an immediate operand. The register instructions are:

   and    Rd, Rs, Rt
   or     Rd, Rs, Rt
   nor    Rd, Rs, Rt
   xor    Rd, Rs, Rt

In each of these instructions the operation is performed on all 32 bits of Rs and Rt, and the result is placed in Rd.

The immediate insructions are:

   andi   Rt, Rs, immed
   ori    Rt, Rs, immed
   xori   Rt, Rs, immed

The immediate operand is zero-extended from 16 to 32 bits (it is padded with leading zeros.



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2009