|< C/C++ Statements, Expressions & Operators 2 | Main | Let Have A Break! >| Site Index | Download |


 

 

 

 

 

MODULE 3b

FINAL PART C/C++ STATEMENTS, EXPRESSIONS

AND OPERATORS 3

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

The source code for this Module is: Module 3 C/C++ source codes. and more practice can be obtain from C lab worksheets 5.

 

The C and C++ skills that should be acquired are: Able to understand and use:

  • The Bitwise operators.

  • The Comma Operator.

 

3.11  The Bitwise Operators

  • All data represented internally by computers as sequence of binary digit (bit).

  • Each bit can assume value 0 or 1.  8 bits equal to 1 byte.

  • Bitwise operators used to manipulate the bits of integral operands, to modify the individual bits (bit by bit) rather than the number itself.  Both operands in a bitwise expression must be of an integral type.

  • Table 3.13 is the list of the bitwise operators.

Operator

Description

& (ampersand - bitwise AND)

The bit in the result are set to 1 if the corresponding bits in the two operands are both 1, otherwise it returns 0.

| (pipe - bitwise inclusive OR)

The bit in the result is set to 1 if at least one (either or both) of the corresponding bits in the two operands is 1, otherwise it returns 0.

^ (caret - bitwise exclusive OR)

The bit in the result is set to 1 if exactly one of the corresponding bits in the two operands is 1.

~ (tilde - bitwise complement)

Negation. 0 bit set to 1, and 1 bit set to 0.  Also used to create destructors.

<< (bitwise shift left)

Moves the bit of the first operand to the left by the number of bits specified by the second operand; it discards the far left bit ; fill from the right with 0 bits.

>> (bitwise shift right)

Moves the bit of the first operand to the right by the number of bits specified by the second operand; discards the far right bit; fill from the right with 0 bits.

 

Table 3.13:  Bitwise Operators

  • Bitwise AND, bitwise inclusive OR, bitwise exclusive OR operators compare their two operands bit by bit.

  • &, >>, << are context sensitive. & can also be the pointer reference operator.  >> can also be the input operator in I/O expressions.  << can also be the output operator in I/O expressions.  You will learn this in another Module.

  • The following Table list the &, | and ^ operators examples.

Operand1,  OP1

Operand2, OP2

OP1 & OP2

OP1 | OP2

OP1 ^ OP2

0

0

0

0

0

1

0

0

1

1

0

1

0

1

1

1

1

1

1

0

 

Table 3.14: Bitwise Operation

  • The following is a program example:

// 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');

}

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C C++ bitwise operator

bits to be left shifted << shift count

bits to be right shifted >> shift count

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

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

// 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');

 }

 

Output:

 

C C++ more on bitwise operator

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:

 

 Bitwise AND (&)

 

 Bitwise exclusive OR (^)

 

 Bitwise inclusive OR (|)

 

 

7

Logical operators:

 

 Logical AND (&&)

 

 Logical OR (||)

 

 

8

Comma Operator (,)

3.12    The Comma Operator

x  =  (a ++, b++);

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Program Examples and Experiments

 

 

 

// 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;

}

 

Output:

 

C C++ standard input output

 

// 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;

}

 

Output:

 

C C++ standard input output EOF

 

 

// 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;

}

 

Output:

 

C C++ console program output

 

// 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;

}

 

Output:

 

C/C++ very simple program development

// 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;

}

 

Output:

 

C/C++ statement expression operators Visual C++

/ ******-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

/* 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 & C++ programming tutorials

 

 

 

 

 

 

 

 

 

 

 

 

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.

 

 

 

 

 

 

 

|< C/C++ Statements, Expressions & Operators 2 | Main | Let Have A Break! >| Site Index | Download |


C and C++ Statements, Expressions & Operators:  Part 1 | Part 2 | Part 3 |