Part- I
Exact Size Integer Types
Logical Bitwise Operators
Shift Operators
 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.
Exact Size Integer Types
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.
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
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);
}
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
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);
}
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
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);
}
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
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:
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);
}
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
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
y = 2
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);
}
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
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
 In a school’s computer lab, a small 8-bit register keeps track of
the status of lab equipment.
 Each bit shows whether something is ON or OFF.
Bit Equipment When Bit = 1 (ON)
0 Main Lights ON
1 Projector ON
2 Computer Systems ON
3 Fans ON
4 Air Conditioner (AC) ON
5 Printer ON
6 Server Connection Active
7 Power Supply Working
Bit Details
Write a C program that:
Takes the lab’s status number (0–255) as input.
Checks each bit using bitwise operators (& and <<).
Prints messages according to these rules:
Rules:
If Power Supply (bit 7) is 0 print "NO POWER!"
→
If Server (bit 6) is 0 but Power Supply is ON print "SERVER
→
DISCONNECTED!"
If any of bits 0–5 are ON print "EQUIPMENT RUNNING"
→
If all bits 0–6 are 0 print "LAB IS OFF"
→
 Click here for code + description
Microsoft Word
Document

BitWise and, or, xor, norOperators in C.ppt

  • 2.
    Part- I Exact SizeInteger Types Logical Bitwise Operators Shift Operators
  • 3.
     Exact SizeInteger 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.
  • 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.
  • 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
  • 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); }
  • 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 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); }
  • 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 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); }
  • 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 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 ones 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:
  • 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); }
  • 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 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 y = 2 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); }
  • 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 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
  • 19.
     In aschool’s computer lab, a small 8-bit register keeps track of the status of lab equipment.  Each bit shows whether something is ON or OFF. Bit Equipment When Bit = 1 (ON) 0 Main Lights ON 1 Projector ON 2 Computer Systems ON 3 Fans ON 4 Air Conditioner (AC) ON 5 Printer ON 6 Server Connection Active 7 Power Supply Working Bit Details
  • 20.
    Write a Cprogram that: Takes the lab’s status number (0–255) as input. Checks each bit using bitwise operators (& and <<). Prints messages according to these rules: Rules: If Power Supply (bit 7) is 0 print "NO POWER!" → If Server (bit 6) is 0 but Power Supply is ON print "SERVER → DISCONNECTED!" If any of bits 0–5 are ON print "EQUIPMENT RUNNING" → If all bits 0–6 are 0 print "LAB IS OFF" →
  • 21.
     Click herefor code + description Microsoft Word Document