1 / 13

Bitwise Operators in C

Bitwise Operators in C. Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int , short, long. Bitwise Operators in C. There are six bit operators: bitwise AND(&) bitwise OR(|) bitwise XOR(^) bitwise complement(~)

tyson
Download Presentation

Bitwise Operators in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bitwise Operators in C

  2. Bitwise Operators in C • Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.

  3. Bitwise Operators in C • There are six bit operators: • bitwise AND(&) • bitwise OR(|) • bitwise XOR(^) • bitwise complement(~) • left shift(<<) • right shift(>>)

  4. Bitwise Operators in C • Bitwise and: c1 & c2 • # include<stdio.h> • main() • { • char c1 = 4,c2 = 6,c3 ; • c3 = c1 & c2; • printf("\n Bitwise AND i.e. c1 & c2 = %d",c3); • } • Bitwise AND i.e. c1 & c2 = 4 • Suppose c1 = 4, c2 = 6; • The value of c1 & c2 is interpreted: • 0000 0100 • & 0000 0110 • ---------------- • 0000 0100

  5. Bitwise Operators in C • Bitwise or: c1 | c2 • # include<stdio.h> • main() • { • char c1 = 4,c2 =6 ,c3 = 3; • c3 = c1 | c2; • printf("\n Bitwise OR i.e. c1 | c2 = %c",c3); • } • Bitwise OR i.e. c1 | c2 = ? • Suppose c1 = 4, c2 = 6; • The value of c1 | c2 is interpreted as follows: • 0000 0100 • | 0000 0110 • -------------- • 0000 0110

  6. Bitwise Operators in C • Bitwise XOR: c1 ^ c2 • # include<stdio.h> • main() • { • char c1 = 4,c2 = 6,c3 = 3; • c3 = c1 ^ c2; • printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3); • } • Suppose c1 = 4, c2 = 6; • The value of c1 ^ c2 is interpreted as follows: • 0000 0100 • ^ 0000 0110 • --------------- • 0000 0010

  7. Bitwise Operators in C • Complement: ~ • # include<stdio.h> • main() • { • char c1 = 4,c2 = 6,c3 = 3; • c3 = ~c1; • printf("\n ones complement of c1 = %c",c3); • } • Suppose c1 = 4, c2 = 6; • The value of ~ c1 is interpreted as follows: • ~ 0000 0100 • --------------- • 1111 1011

  8. Bitwise Operators in C • Left shift operator • # include<stdio.h> • main() • { • char c1 = 1,c2 = 2,c3 = 3; • c3 = c1<<2; • printf("\n left shift by 2 bits c1 << 2 = %c",c3); • } • c3 = c1 << 2; • The bits are shifted left by two places. • c1 is 0000 0100 • It is shifted 2 bits to the left • 0001 00** • While shifting, the high-order (left) bits are discarded. • The vacuum on the right side is filled with 0s.

  9. Bitwise Operators in C • Right shift operation • # include<stdio.h> • main() • { • char c1 = 1,c2 = 2,c3 = 3; • c3 = c1>>2; • printf("\n right shift by 2 bits c1 >> 2 = %c",c3); • }

  10. Bitwise Operators in C • Bitwise AND • xi yi xi &1 yi • 0 0 0 • 0 1 0 • 1 0 0 • 1 1 1 • Variable b3 b2 b1 b0 • x 1 1 0 0 • y 1 0 1 0 • z = x & y 1 0 0 0

  11. Bitwise Operators in C • Bitwise OR • xi yi xi |1 yi • 0 0 0 • 0 1 1 • 1 0 1 • 1 1 1 • Variable b3 b2 b1 b0 • x 1 1 0 0 • y 1 0 1 0 • z = x | y 1 1 1 0

  12. Bitwise Operators in C • Bitwise XOR • xi yi xi ^1 yi • 0 0 0 • 0 1 1 • 1 0 1 • 1 1 0 • Variable b3 b2 b1 b0 • x 1 1 0 0 • y 1 0 1 0 • z = x ^ y 0 1 1 0

  13. Bitwise Operators in C • Bitwise NOT • xi ~1 xi • 0 1 • 1 0 • Variable b3 b2 b1 b0 • x 1 1 0 0 • z = ~x 0 0 1 1

More Related