By Y N D ARAVIND Associate Professor, Dept Of CSE Newton’s Group Of Institutions, Macherla @2020 Presented By Mr.Y N D Aravind 1
Part- I Exact Size Integer Types Logical Bitwise Operators Shift Operators @2020 Presented By Mr.Y N D Aravind 2
Exact Size Integer Types  The integer can be of two types – Unsigned integers and signed integers.  They can be of four types –short int, int, long int, long long int.  As integer is one of the datatype it is built in so it is machine dependent.  The size of int may be 4 bytes on computer and on another it is 2 bytes.  In other applications we need to fix the size of integers.  C allows us to define integer types of sizes 8 bits, 16 bits, 32 bits and 64 bits.  They are defined in stdint.h header file. Presented By Mr.Y N D Aravind 3
Exact Size Integer Types Presented By Mr.Y N D Aravind 4
Bitwise operators are used for manipulation of data at bit level. Operator Meaning & Bitwise AND. | Bitwise OR. ^ Bitwise Exclusive OR. << Shift left. >> shift right. ~ One’s complement. These bits are used to test the bits, or shifting them right to left. These operators may not be applied to float or double. These operators are works on integers and characters. Presented By Mr.Y N D Aravind 5
Bits are numbered from zero onwards increasing order from right to left. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 16-bit integer 7 6 5 4 3 2 1 0 8- bit integer Presented By Mr.Y N D Aravind 6
The truth table for Bitwise AND is A B A & B 0 0 0 0 1 0 1 0 0 1 1 1 Program:- Write a program to use bitwise AND operator between two integer. Source code:- #include <stdio.h> main() { int a,b; printf(“n Enter a, b values”); scanf(“%d %d”,&a,&b); printf(“n After bitwise AND is %d ”,a&b); } Presented By Mr.Y N D Aravind 7
Enter a, b values 3 2 After bitwise AND is 2 To perform Bitwise AND operator:- a=3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 b=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a & b 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a & b =2 Presented By Mr.Y N D Aravind 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
The truth table for Bitwise OR is A B A | B 0 0 0 0 1 1 1 0 1 1 1 1 Program:- Write a program to use bitwise OR operator between two integer. Source code:- #include <stdio.h> main() { int a,b; printf(“n Enter a, b values”); scanf(“%d %d”,&a,&b); printf(“n After bitwise OR is %d”,a|b); } Presented By Mr.Y N D Aravind 9
Enter a, b values 3 2 After bitwise OR is 3 To perform Bitwise OR operator:- a=3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 b=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a | b 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a | b =3 Presented By Mr.Y N D Aravind 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
The truth table for Bitwise EXCLUSIVE OR is A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0 Program:- Write a program to use bitwise Exclusive OR operator between two integer. Source code:- #include <stdio.h> main() { int a,b; printf(“n Enter a, b values”); scanf(“%d %d”,&a,&b); printf(“n After bitwise ECLUSIVE OR is %d”,a ^ b); } Presented By Mr.Y N D Aravind 11
Enter a, b values 3 2 After bitwise Exclusive OR is 1 To perform Bitwise Exclusive OR operator:- a=3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 b=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a ^ b 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a ^ b =1 Presented By Mr.Y N D Aravind 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
The one‟s complement operator is a unary operator that requires one operand only. It complements the bits in the operand. That means it finds the reverse of the operand bit. The result of the operand is 1 if the original bit is 0 and it is 0 if the original bit is 1. The truth table for one‟s complement operator is shown below: Presented By Mr.Y N D Aravind 13
Write a program to shift inputted data by two bits right.. Source code:- #include <stdio.h> main() { int x,y; printf(“n Enter x value”); scanf(“%d”,&x); x>>=2; y=x; printf(“n The right shifted data is =%d”,y); } Presented By Mr.Y N D Aravind 14
Enter x value 8 The right shifted data is = 2 To perform Right shift operator:- x=8 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Right shift 2 bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 y = 2 Presented By Mr.Y N D Aravind 15 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
Write a program to shift inputted data by three bits left. Source code:- #include <stdio.h> main() { int x,y; printf(“n Enter x value”); scanf(“%d”,&x); X<<=3; y=x; printf(“n The left shifted data is =%d”,y); } Presented By Mr.Y N D Aravind 16
Enter x value 2 The left shifted data is = 16 To perform Left shift operator:- x=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Right shift 2 bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 y = 16 Presented By Mr.Y N D Aravind 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
Presented By Mr.Y N D Aravind 18

Bitwise Operators in C

  • 1.
    By Y N DARAVIND Associate Professor, Dept Of CSE Newton’s Group Of Institutions, Macherla @2020 Presented By Mr.Y N D Aravind 1
  • 2.
    Part- I Exact SizeInteger Types Logical Bitwise Operators Shift Operators @2020 Presented By Mr.Y N D Aravind 2
  • 3.
    Exact Size IntegerTypes  The integer can be of two types – Unsigned integers and signed integers.  They can be of four types –short int, int, long int, long long int.  As integer is one of the datatype it is built in so it is machine dependent.  The size of int may be 4 bytes on computer and on another it is 2 bytes.  In other applications we need to fix the size of integers.  C allows us to define integer types of sizes 8 bits, 16 bits, 32 bits and 64 bits.  They are defined in stdint.h header file. Presented By Mr.Y N D Aravind 3
  • 4.
    Exact Size IntegerTypes Presented By Mr.Y N D Aravind 4
  • 5.
    Bitwise operators areused for manipulation of data at bit level. Operator Meaning & Bitwise AND. | Bitwise OR. ^ Bitwise Exclusive OR. << Shift left. >> shift right. ~ One’s complement. These bits are used to test the bits, or shifting them right to left. These operators may not be applied to float or double. These operators are works on integers and characters. Presented By Mr.Y N D Aravind 5
  • 6.
    Bits are numberedfrom zero onwards increasing order from right to left. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 16-bit integer 7 6 5 4 3 2 1 0 8- bit integer Presented By Mr.Y N D Aravind 6
  • 7.
    The truth tablefor Bitwise AND is A B A & B 0 0 0 0 1 0 1 0 0 1 1 1 Program:- Write a program to use bitwise AND operator between two integer. Source code:- #include <stdio.h> main() { int a,b; printf(“n Enter a, b values”); scanf(“%d %d”,&a,&b); printf(“n After bitwise AND is %d ”,a&b); } Presented By Mr.Y N D Aravind 7
  • 8.
    Enter a, bvalues 3 2 After bitwise AND is 2 To perform Bitwise AND operator:- a=3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 b=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a & b 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a & b =2 Presented By Mr.Y N D Aravind 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  • 9.
    The truth tablefor Bitwise OR is A B A | B 0 0 0 0 1 1 1 0 1 1 1 1 Program:- Write a program to use bitwise OR operator between two integer. Source code:- #include <stdio.h> main() { int a,b; printf(“n Enter a, b values”); scanf(“%d %d”,&a,&b); printf(“n After bitwise OR is %d”,a|b); } Presented By Mr.Y N D Aravind 9
  • 10.
    Enter a, bvalues 3 2 After bitwise OR is 3 To perform Bitwise OR operator:- a=3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 b=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a | b 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a | b =3 Presented By Mr.Y N D Aravind 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
  • 11.
    The truth tablefor Bitwise EXCLUSIVE OR is A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0 Program:- Write a program to use bitwise Exclusive OR operator between two integer. Source code:- #include <stdio.h> main() { int a,b; printf(“n Enter a, b values”); scanf(“%d %d”,&a,&b); printf(“n After bitwise ECLUSIVE OR is %d”,a ^ b); } Presented By Mr.Y N D Aravind 11
  • 12.
    Enter a, bvalues 3 2 After bitwise Exclusive OR is 1 To perform Bitwise Exclusive OR operator:- a=3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 b=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a ^ b 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 a ^ b =1 Presented By Mr.Y N D Aravind 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
  • 13.
    The one‟s complementoperator is a unary operator that requires one operand only. It complements the bits in the operand. That means it finds the reverse of the operand bit. The result of the operand is 1 if the original bit is 0 and it is 0 if the original bit is 1. The truth table for one‟s complement operator is shown below: Presented By Mr.Y N D Aravind 13
  • 14.
    Write a programto shift inputted data by two bits right.. Source code:- #include <stdio.h> main() { int x,y; printf(“n Enter x value”); scanf(“%d”,&x); x>>=2; y=x; printf(“n The right shifted data is =%d”,y); } Presented By Mr.Y N D Aravind 14
  • 15.
    Enter x value8 The right shifted data is = 2 To perform Right shift operator:- x=8 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Right shift 2 bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 y = 2 Presented By Mr.Y N D Aravind 15 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
  • 16.
    Write a programto shift inputted data by three bits left. Source code:- #include <stdio.h> main() { int x,y; printf(“n Enter x value”); scanf(“%d”,&x); X<<=3; y=x; printf(“n The left shifted data is =%d”,y); } Presented By Mr.Y N D Aravind 16
  • 17.
    Enter x value2 The left shifted data is = 16 To perform Left shift operator:- x=2 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Right shift 2 bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 y = 16 Presented By Mr.Y N D Aravind 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
  • 18.
    Presented By Mr.YN D Aravind 18