|
| My Training Period: xx hours
The source code for this Module is:Module 3 C/C++ source codes. and more practice can be obtain fromC lab worksheets 5.
The C and C++ skills that should be acquired are: Able to understand and use:
3.11 The Bitwise Operators
| ||||||||||||||||||||||||||||||||||||||||||||||
// bitwise operators
#include <stdio.h>
int main()
{
unsigned p;
// function prototype…
void DisplayBits(unsigned);
printf("Enter an unsigned integer: ");
scanf("%u", &p);
DisplayBits(p);
return 0;
}
// function definition…
void DisplayBits(unsigned number)
{
unsigned q;
// 2 byte, 16 bits position
// operated bit by bit and hide/mask other bits
// using left shift operator
// start with 10000000 00000000
unsigned DisplayMask = 1<<15;
printf("%7u = ", number);
for(q = 1; q < 16; q++)
{
// combining variable number with variable DisplayMask
putchar(number & DisplayMask ? '1':'0');
// number variable is left shifted one bit
number<<= 1;
// separate by 8 bits position (1 byte)
if(q % 8 == 0)
putchar(' ');
}
putchar('\n');
}

Change DisplayMask = 0<<15 and & to | for inclusive OR operation and rerun the program.
The shift general syntax:
bits to be left shifted << shift count
bits to be right shifted >> shift count
The shift count must be non-negative and less than the number of bits required to represent the data type of the left operand.
For example, let say the variable declaration is:
unsigned int p = 5
In binary, p = 00000000 00000101
For p<<1 in binary,
00000000 00000101 << 1 = 00000000 00001010 = 10 decimal
For p<<15 in binary,
00000000 00000101 << 15 = 10000000 00000000 = 32768 decimal
For right shift, while bits are shifted toward low-order position, 0 bits enter the high-order positions, if the data is unsigned. If the data is signed and the sign bit is 0, then 0 bits also enter the high-order positions.
If the sign bit is 1, the bits entering high-order positions are implementation dependent, on some machines (processor architecture) 1s, and on others 0s, are shifted in.
The former type of operation is known as the arithmetic right shift, and the latter type is the logical right shift. So, for portability issue, these operators should only be used on unsigned operands.
For example:
unsigned int p = 40960, for 16 bits,
In binary, p = 10100000 00000000
For p >> 1 in binary,
10100000 00000000 >> 1 = 01010000 00000000 = 20480 decimal
For p>>15 in binary,
10100000 00000000 >> 15 = 00000000 00000001 = 1 decimal
A program example:
// bitwise operators
#include <stdio.h>
// function prototype, you will learn this later
void BitwiseOp(unsigned int);
int main()
{
unsigned int num1, num2, num3, mask, SetBit;
num1 = 7535;
mask = 1;
printf("The result of ANDing the following numbers\n");
printf("using the bitwise AND, & is\n");
// display in normal and binary representation
BitwiseOp(num1);
BitwiseOp(mask);
printf(" ------------------------\n");
// bitwise AND operation
BitwiseOp(num1 & mask);
num1 = 15;
SetBit = 241;
printf("\nThe result of inclusive ORing the following numbers\n");
printf("using the bitwise inclusive OR, | is\n");
BitwiseOp(num1);
BitwiseOp(SetBit);
printf(" ------------------------\n");
// bitwise inclusive OR operation
BitwiseOp(num1 | SetBit);
num1 = 249;
num2 = 299;
printf("\nThe result of exclusive ORing the following numbers\n");
printf("using the bitwise exclusive OR, ^ is\n");
BitwiseOp(num1);
BitwiseOp(num2);
// bitwise exclusive OR operation
printf(" ------------------------\n");
BitwiseOp(num1 ^ num2);
num3 = 21321;
printf("\nThe One's complement of\n");
BitwiseOp(num3);
printf(" |||||||| ||||||||\n");
// one's complement operation
BitwiseOp(~num3);
return 0;
}
// function definition…
void BitwiseOp(unsigned int value)
{
unsigned int p;
// two 8 bits, 16 position, shift to left
unsigned int DisplayMask = 1 << 15;
printf("%7u = ", value);
// Loop for all bit...
for(p=1; p<=16; p++)
{
// if TRUE set to '1', otherwise set to '0'
putchar(value & DisplayMask ? '1':'0');
// shift to left bit by bit
// equal to: value << + 1 statement
value <<= 1;
if(p % 8 == 0)
// put a space…
putchar(' ');
}
putchar('\n');
}

For C++, the following is a list of the binary operators’ categories that can be used in C++ expressions.
1 | (Compound) Assignment operators: |
| Assignment (=) |
| Addition assignment (+=) |
| Subtraction assignment (–=) |
| Multiplication assignment (*=) |
| Division assignment (/=) |
| Modulus assignment (%=) |
| Left shift assignment (<<=) |
| Right shift assignment (>>=) |
| Bitwise AND assignment (&=) |
| Bitwise exclusive OR assignment (^=) |
| Bitwise inclusive OR assignment (|=) |
|
|
2 | Additive operators: |
| Addition (+) |
| Subtraction (–) |
|
|
3 | Multiplicative operators: |
| Multiplication (*) |
| Division (/) |
| Modulus (%) |
|
|
4 | Shift operators: |
| Right shift (>>) |
| Left shift (<<) |
|
|
5 | Relational and equality operators: |
| Less than (<) |
| Greater than (>) |
| Less than or equal to (<=) |
| Greater than or equal to (>=) |
| Equal to (==) |
| Not equal to (!=) |
|
|
6 | Bitwise operators: |
| BitwiseAND (&) |
| Bitwise exclusiveOR (^) |
| Bitwise inclusiveOR (|) |
|
|
7 | Logical operators: |
| LogicalAND (&&) |
| LogicalOR (||) |
|
|
8 | Comma Operator (,) |
Furthermore in C++ the <bitset> header defines a bitset template class and two supporting template functions for representing and manipulating fixed-size sequences of bits in more efficient way.
Frequently used as a simple punctuation mark.
Used to separate variable declarations, functions’ arguments, etc.
Also can acts as an operator.
Separating two sub expressions with a comma can form an expression.
Then, both expressions are evaluated, left evaluated first. The entire expression evaluates as the value of the right expression.
For example:
x = (a ++, b++);
Assigns the value of b to x, then increments a, and then increments b.
As conclusion, table 3.15 is the summary of the operators’ precedence.
Operators | Associativity |
( ) [ ] → . | Left to right |
! ~ ++ -- + - * & (type) sizeof() | Right to left |
* / % | Left to right |
+ - | Left to right |
<< >> | Left to right |
< <= > >= | Left to right |
== != | Left to right |
& | Left to right |
^ | Left to right |
| | Left to right |
&& | Left to right |
|| | Left to right |
?: | Right to left |
= += -= *= /= %= &= ^= |= <<= >>= , | Right to left |
(space) | Left to right |
The operators are shown in decreasing order of precedence from top to bottom | |
Table 3.15: Summary of the operators precedence | |
For this Table it is read from top (the highest) to bottom (the lowest). Then if they are at the same level, we read it from left (the highest) to right (the lowest).
// program copying from standard input, keyboard to standard output, console
// using pre defined functions, getchar() and putchar(), defined in stdio.h header file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count;
// gives some prompt...
printf("Enter a line of text:\n");
printf("Press \'S\' to stop.\n");
// get character from standard input
// store in variable count
count = getchar();
// while the S is not encountered...
while(count != 'S')
{
// put character on the standard output
putchar(count);
// carry on getting character from the standard input
count = getchar();
}
return 0;
}

// copying from standard input, keyboard to standard output, console
// using pre defined functions, getchar() and putchar() with End Of File, EOF.
// EOF may be system dependent
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count;
// gives some prompt...
printf("Enter a line of text:\n");
printf("EOF to stop.\n");
// get character from standard input
// store in variable count
count = getchar();
// while the End Of File is not encountered...
while(count != EOF)
{
// put character on the standard output
putchar(count);
// carry on getting character from the standard input
count = getchar();
}
return 0;
}

// creating the working program skeleton…
#include <stdio.h>
int main()
{
// printf("Some prompt here...\n");
int count, charnum = 0;
while ((count = getchar()) != EOF)
{
if(count != ' ')
++charnum;
}
printf("test the output here...\n");
return 0;
}

// add other functionalities by following the
// simple steps in program development…
#include <stdio.h>
int main()
{
// printf("Some prompt here...\n");
// -----In the process: declare and initialize ----------
// -----each variable used------------------------
// -----Third: compile and run----------------
// -----Fourth: If there are errors, recompile and rerun----
// -----Finally, if there is no error, complete other part of-----
// -----the program, such as comments etc-------------
int count, charnum = 0, linenum = 0;
printf("Enter several line of texts.\n");
printf("Press Carriage Return then EOF to end.\n\n");
// -------------First step: build the loop-----------
// while storing the character process
// not equal to the End Of File...
while((count = getchar()) != EOF)
{
// do the character count
if(count != ' ')
++charnum;
// and the line count...
if(count == '\n')
{
++linenum;
charnum = charnum -1;
}
}
// ----------Second step: test the output---------------
printf("The number of line = %d\n", linenum);
printf("The number of char = %d\n", charnum);
return 0;
}

// add other functionalities by following the simple steps in program development…
#include <cstdio>
int main()
{
// printf("Some prompt here...\n");
// -----In the process: declare and initialize ----------
// -----each variable used------------------------
// -----Third: compile and run----------------
// -----Fourth: If there are errors, recompile and rerun----
// -----Finally, if there is no error, complete other part of-----
// -----the program, such as comments etc-------------
int count, charnum = 0, linenum = 0;
printf("Enter several line of texts.\n");
printf("Press Carriage Return then EOF to end.\n\n");
// -------------first: build the loop-----------
// while storing the character process
// not equal to the End Of File...
while((count = getchar()) != EOF)
{
// do the character count
if(count != ' ')
++charnum;
// and the line count...
if(count == '\n')
{
++linenum;
charnum = charnum -1;
}
}
// ----------second: test the output---------------
printf("The number of line = %d\n", linenum);
printf("The number of char = %d\n", charnum);
return 0;
}

The following are program examples compiled usinggcc.
/ ******-cpoundassig.c-******* /
#include <stdio.h>
int main()
{
int a = 10, b = 20;
printf("Initially: a = 3, b = 4\n");
printf("\na += b ---> a = a + b = %d\n", a+=b);
printf("a last value = %d\n", a);
printf("\na *= b ---> a = a * b = %d\n", a*=b);
printf("a last value = %d\n", a);
printf("\na -= b ---> a = a - b = %d\n", a-=b);
printf("a last value = %d\n", a);
printf("\na/=b ---> a = a / b = %d\n", a/=b);
printf("a last value = %d\n", a);
printf("\na-=(b+1)---> a = a - (b + 1) = %d\n", a-=(b+1));
printf("a last value = %d\n", a);
return 0;
}
[bodo@bakawali ~]$ gcc cpoundassig.c -o cpoundassig
[bodo@bakawali ~]$ ./cpoundassig
Initially: a = 3, b = 4
a += b ---> a = a + b = 30
a last value = 30
a *= b ---> a = a * b = 600
a last value = 600
a -= b ---> a = a - b = 580
a last value = 580
a/=b ---> a = a / b = 29
a last value = 29
a-=(b+1)---> a = a - (b + 1) = 8
a last value = 8
Another example.
/* bitwise operators */
/******--bitwise.c--******/
#include <stdio.h>
int main()
{
unsigned p;
/* function prototype. */
void DisplayBits(unsigned);
printf("Enter an unsigned integer: ");
scanf("%u", &p);
DisplayBits(p);
return 0;
}
/* function definition. */
void DisplayBits(unsigned number)
{
unsigned q;
/* 2 byte, 16 bits position */
/* operated bit by bit and hide/mask other bits */
/* using left shift operator */
/* start with 10000000 00000000 */
unsigned DisplayMask = 1<<15;
printf("%7u = ", number);
for(q = 1; q < 16; q++)
{
/* combining variable number with variable DisplayMask */
putchar(number & DisplayMask ? '1':'0');
/* number variable is left shifted one bit */
number<<= 1;
/* separate by 8 bits position (1 byte) */
if(q % 8 == 0)
putchar(' ');
}
putchar('\n');
}
[bodo@bakawali ~]$ gcc bitwise.c -o hahahahaha
[bodo@bakawali ~]$ ./hahahahaha
Enter an unsigned integer: 10
10 = 00000000 0000101
[bodo@bakawali ~]$ ./hahahahaha
Enter an unsigned integer: 200
200 = 00000000 1100100
C and C++ further reading and digging:
1. Check the best selling C/C++ books at Amazon.com.
2. The source code for this Module is:Module 3 C/C++ source codes. and the related worksheet for your practice is:C lab worksheets.