You are on page 1of 15

Operators:

Bitwise
Procedural Programming

Mario Simaremare, S.Kom., M.Sc.


Program Studi Sarjana Sistem Informasi
Institut Teknologi Del
Objectives

• The objective of this session is the following:


• students are able to elaborate the role of operators.
• students are able to use bitwise operators.

Procedural Programming 2
Outlines

1. Operator.
2. Bitwise operators.

Procedural Programming 3
Operator

Procedural Programming 4
Operator

• Solution = algorithm + data.


• Operation is needed to use and manipulate data.
• By following the algorithm.

• Operators operate one or two operands in an operation.


• One-operand operator is also called as unary operator.
• Two-operand operator is called as binary operator.

• Different data types might be operated differently,


hence different operators are needed.
Procedural Programming 5
Bitwise Operators

Procedural Programming 6
Bitwise operators

Bitwise Test Operator These operators operate values in


AND & bit-level.
OR |
XOR operation will return:
Exclusive OR (XOR) ^ • 1 when the two bits are different,
NOT ~ • 0 when the two bits are equal.

Right shift >>


Left shift <<

Procedural Programming 7
Bitwise operations
#include <stdio.h> Applying bitwise operations
to 8-bit char values
int main(int _argc, char **_argv) {
char c1 = 'f'; // 102 -> 01100110
char c2 = '4'; // 52 -> 00110100
char c3 = c1 & c2; // 00100100 -> 36 ASCII '$'
char c4 = c1 ^ c2; // 01010010 -> 82 ASCII 'R'
char c5 = c1 >> 1; // 00110011 -> 51 ASCII '3'

unsigned short int i1 = 15; // 00000000 00001111


unsigned short int i2 = ~i1; // 11111111 11110000 -> 65520

return 0; Playing with 16-bits integer values


}

Procedural Programming 8
Bitwise operations: AND

char c1 = 'f'; // 102 -> 01100110


char c2 = '4'; // 52 -> 00110100 Will only return 1 when
& the two bits are all 1

char c3 = c1 & c2; // 00100100 -> 36 ASCII '$'

Procedural Programming 9
Bitwise operations: XOR

char c1 = 'f'; // 102 -> 01100110


char c2 = '4'; // 52 -> 00110100 Will only return 1 when
^ the two bits are different

char c4 = c1 ^ c2; // 01010010 -> 82 ASCII 'R'

Procedural Programming 10
Bitwise operations: right shift

char c1 = 'f'; // 102 -> 01100110 Shift the bits to


the right side
>>1
char c5 = c1 >> 1; // 001100110 -> 51 ASCII '3'
>>1 The last bit is removed and
the other higher significant
Added a leading 0 to make it 8-bits 000110011 bits are shifted back

Doing more right shift


operation

Procedural Programming 11
Bitwise operations: NOT

unsigned short int i1 = 15; // 00000000 00001111 Simply reverse


~ the bit values
unsigned short int i2 = ~i1; // 11111111 11110000 -> 65520

All 0s turns to 1s,


Decimal value
and 1s to 0s

Procedural Programming 12
Todos

• Bitwise operators work in the bit level.


• Seeing value in one-zero sequence.

• The operator looks very similar to the logical operators.


• Try not to be tempted to use them in logical operations.

• Conduct some experiments to strengthen your understanding.

Procedural Programming 13
References

• Kerninghan & Ritchie. The C Programming Language.


• Herbert Schildt. C: The Complete Reference.

Procedural Programming 14
Thank
you

Procedural Programming 15

You might also like