Docsity

Bitwise Operators - Window Programming - Lecture Slides, Slides of Windows Programming

Partial preview of the text

Download Bitwise Operators - Window Programming - Lecture Slides and more Slides Windows Programming in PDF only on Docsity!

Windows Programming

Lecture 06

Bitwise Operators

  • An operator that manipulates individual bits. The operators that most people are familiar with, such as the addition operator (+), work with bytes or groups of bytes. Occasionally, however, programmers need to manipulate the bits within a byte. The C programming language supports the following bitwise operators:
  • >> Shifts bits right
  • << Shifts bits left
  • & Does an AND compare on two groups of bits
  • | Does an OR compare on two groups of bits
  • ^ Does an XOR compare on two groups of bits
  • ~ Complements a group of bits

Bitwise AND Operator

There are two kinds of AND operators in the C language: the logical AND, and the bitwise AND. The former is represented by the && operator, and the latter uses the & operator.

The bitwise AND works very much the same way, except that it works with two bits instead of two expressions. The bitwise AND operation returns 1 if and only if both of its operands are equal to 1. In other words, we have the following truth table for the bitwise AND.

Bitwise AND Operator

0 AND 0 = 0

0 AND 1 = 0 x AND 0 = 0

1 AND 0 = 0 x AND 1 = x 1 AND 1 = 1

Bitwise OR Operator

0 OR 0 = 0

0 OR 1 = 1 x OR 0 = x

1 OR 0 = 1 x OR 1 = 1

1 OR 1 = 1

XOR Operator

The XOR is a little strange because there is no logical equivalent for it in C, even though many languages include one. The XOR operation is symbolized by the ^ character in C. The term XOR stands for "exclusive OR“, and means "one or the other, but not both." In other words, XOR returns 1 if and only if exactly one of its operands is 1. If both operands are 0, or both are 1, then XOR returns 0.

Bitwise Shift Operators

There are two bitwise shift operators, namely shift left and shift right. In C, they are represented by the << and >> operators, respectively. These operations are very simple, and do exactly what they say: shift bits to the left or to the right.

Bitwise Left Shift (<<) Operator

Bitwise Left-Shift is useful when to want to MULTIPLY an integer (not floating point numbers) by a power of 2. The operator, like many others, takes 2 operands like this: a << b This expression returns the value of a multiplied by 2 to the power of b.

Bitwise Right Shift (>>) Operator

Bitwise Right-Shift does the opposite, and takes away bits on the right. Suppose we had: a >> b This expression returns the value of a divided by 2 to the power of b.

Example: 8 >> 2. 8 is 1000 in binary. Performing a right shift of 2 involves knocking the last 2 bits off: 1000, which leaves us with 10, i.e. 2. 8 >> 2 is the as doing 8/2*2 = 8/4 = 2.

But what happens if we had a left operand that's not a power of 2? Let's try 9 >> 2. 9 is 1001. Now take off the last 2 bits, leaving us with 10, which is 2. But this does make sense, since 9/4 = 2.25, which rounds down to 2.

Application of Bitwise Operators

Bitwise operators have two main applications. The first is using them to combine several values into a single variable. Suppose you have a series of flag variables which will always have only one of two values: 0 or 1 (this could also be true or false). The smallest unit of memory you can allocate to a variable is a byte, which is eight bits. But why assign each of your flags eight bits, when each one only needs one bit? Using bitwise operators allows you to combine data in this way.

**typedef unsigned long ULONG; typedef ULONG PULONG; typedef unsigned short USHORT; USHORT PUSHORT;

typedef

Macros

#define LOWORD(l) ( (WORD)(l) ) #define HIWORD(l) ( (WORD)( ( (DWORD)(l) >> 16)&0xFFFF) )

int b=2623; // 0xA3F HIWORD(2623) // call to macro

#define LOBYTE(w) ( (BYTE)(w) ) #define HIBYTE(w) ( (BYTE)( ( (WORD)(w) >> 8) & 0xFF) )

Typecasting

char ch;

int i;

… … …

some code here

… … …

i = ch; // no problem

ch = i; // compiler warning

assertions

In C, assertions are implemented with the standard assert macro. The argument to assert must be true when the macro is executed, otherwise the program aborts and prints an error message. For example, the assertion

assert( size <= LIMIT );

will abort the program and print an error message like this:

Assertion violation: file tripe.c, line 34: size <= LIMIT if size is greater than LIMIT.