0% found this document useful (0 votes)
4 views

Bitwise Operations Full Clean

Uploaded by

contact.baaziz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Bitwise Operations Full Clean

Uploaded by

contact.baaziz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bitwise operations in C language

Introduction:

All information stored in a computer's memory is in the form of a series of bits. For example, the

integer 10 (in decimal base) stored as a 16-bit two's complement number will be encoded by the

following bit sequence:

0000 0000 0000 1010

When we talk about the position of a bit in a binary number, index 0 corresponds to the rightmost bit

(also called Least Significant Bit, or LSB), index 1 to the bit to the left of the lowest bit, and so on.

The leftmost bit of the binary number is called the Most Significant Bit (MSB).

In C language, you can write a number in binary (base 2) by prefixing it with 0b. For example, if we

want to represent the number 26 in binary in eight positions (i.e. 8-bit representation), in C language

this gives 0b00011010. In hexadecimal (base 16), this is equivalent to 0x1A. Recall that, in C

language, hexadecimal numbers are prefixed with 0x.

On the other hand, note that on some machines or operating systems, an int could use 2, 4 or 8

bytes (so 16, 32 or 64 bits). Therefore, if we want to declare variables with a determined number of

bits, the C language introduces a new type class:

- int8_t (8-bit signed integer)

- uint8_t (8-bit unsigned integer)

- uint16_t (16-bit unsigned integer)


These types are defined in the stdint.h header and guarantee that the variables declared thus have

the desired number of bits regardless of the machine or operating system used.

Bitwise operators:

"Bitwise operators" in C language allow you to set, modify or test one or more bits of data. These

operators are:

NOT "~"

AND "&"

OR "|"

XOR (eXclusive OR) "^"

SHL (SHift Left) "<<"

SHR (SHift Right) ">>"

NOT operator "~"

The unary operator NOT flips the state of a bit according to the following table:

ANOT A

01

10

Example:

uint16_t a = 1; /* <=> a = 0b0000000000000001; */

uint16_t b = ~a; /* <=> b = 0b1111111111111110; */

AND operator "&"


The binary operator AND combines the state of two bits according to the following table:

ABA AND B

000

010

100

111

Example:

uint16_t a = 0xF0F0; /* <=> a = 0b1111000011110000; */

uint16_t b = 0x00FF; /* <=> b = 0b0000000011111111; */

uint16_t c = a & b; /* <=> c = 0b0000000011110000; that's 0x00F0 in hexadecimal */

OR operator "|"

The binary operator OR combines the state of two bits according to the following table:

ABA OR B

000

011

101

111

Example:

uint16_t a = 0xF0F0; /* <=> a = 0b1111000011110000; */

uint16_t b = 0x00FF; /* <=> b = 0b0000000011111111; */

uint16_t c = a | b; /* <=> c = 0b1111000011111111; that's 0xF0FF in hexadecimal */


XOR (eXclusive OR) operator "^"

The binary operator XOR combines the state of two bits according to the following table:

ABA XOR B

000

011

101

110

Example:

uint16_t a = 0xF0F0; /* <=> a = 0b1111000011110000; */

uint16_t b = 0x00FF; /* <=> b = 0b0000000011111111; */

uint16_t c = a ^ b; /* <=> c = 0b1111000000001111; that's 0xF00F in hexadecimal */

SHR (SHift Right) operator ">>"

result = op_1 >> op_2

This operator allows to shift a given operand op_1 by a certain number of bits to the right (the

amount of shift is specified by op_2).

Example:

uint16_t a = 0xF0F0; /* <=> a = 0b1111000011110000; */

uint16_t b = 2; /* <=> b = 0b0000000000000010; */

uint16_t c = a >> b; /* <=> c = 0b0011110000111100; that's 0x3C3C in hexadecimal */

SHL (SHift Left) operator "<<"


result = op_1 << op_2

This operator allows to shift a given operand op_1 by a certain number of bits to the left.

Example:

uint16_t a = 0xF0F0; /* <=> a = 0b1111000011110000; */

uint16_t b = 2; /* <=> b = 0b0000000000000010; */

uint16_t c = a << b; /* <=> c = 0b1100001111000000; that's 0xC3C0 in hexadecimal */

Using Bitwise Operations

Set a bit in a value:

Example:

uint16_t a = 0x000F; /* <=> a = 0b0000000000001111; */

uint16_t b = 0x0010; /* <=> b = 0b0000000000010000; b is our mask! */

uint16_t c = a | b; /* <=> c = 0b0000000000011111; that's 0x001F in hexadecimal */

Clear a bit in a value:

Example:

uint16_t a = 0x000F; /* <=> a = 0b0000000000001111; */

uint16_t b = 0xFFF7; /* <=> b = 0b1111111111110111; b is our mask! */

uint16_t c = a & b; /* <=> c = 0b0000000000000111; that's 0x0007 in hexadecimal */

Read the state of a bit in a value:

Example:

uint16_t a = 0x000F; /* <=> a = 0b0000000000001111; */

if (a & (1u << 2))

puts("bit 2 == 1");
else

puts("bit 2 == 0");

You might also like