C and C++ abilities should be acquired:
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
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 <>
{
// 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:
-
Change DisplayMask = 0<<15>
-
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
// 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 <<>
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:
-
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: |
|
| Bitwise AND (&) |
|
| Bitwise exclusive OR (^) |
|
| Bitwise inclusive OR (|) |
|
|
|
7 | Logical operators: |
|
| Logical AND (&&) |
|
| Logical OR (||) |
|
|
|
8 | Comma Operator (,) |
-
Furthermore in C++ the
header defines a bitset template class and two supporting template functions for representing and manipulating fixed-size sequences of bits in more efficient way.
3.12 The Comma Operator
-
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 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
#include
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:
// 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
#include
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:
-------------------------------------------------------------------
// creating the working program skeleton…
#include
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:
// add other functionalities by following the
// simple steps in program development…
#include
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:
// add other functionalities by following the simple steps in program development…
#include
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:
-
The following are program examples compiled using gcc.
/ ******-cpoundassig.c-******* /
#include
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
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 <>
{
/* 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
No comments:
Post a Comment