0% found this document useful (0 votes)
1 views21 pages

C Chapter 12 Bitwise

The document discusses bit fields and bitwise operations, detailing how bitwise operators manipulate bits in integer types such as int and char. It covers various operations like AND, OR, XOR, and bit shifting, along with examples and applications for setting and getting bits. Additionally, it includes programming exercises for converting binary to decimal and counting trailing zeros in binary representations.

Uploaded by

b.kircali55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views21 pages

C Chapter 12 Bitwise

The document discusses bit fields and bitwise operations, detailing how bitwise operators manipulate bits in integer types such as int and char. It covers various operations like AND, OR, XOR, and bit shifting, along with examples and applications for setting and getting bits. Additionally, it includes programming exercises for converting binary to decimal and counting trailing zeros in binary representations.

Uploaded by

b.kircali55
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Bit Fields & Bitwise Operations

Bit Fields & Bitwise 1


Operations
Bitwise Structure
• Bitwise operators allow you to read and manipulate bits in variables
of certain types.
• Bitwise operators only work on a limited number of types: int and
char. Apply to all kinds of integer types:
– signed and unsigned
– char, short, int, long, long long
• Bitwise operators works with bits, following is bit representation of
byte.

2
Review – Bitwise Operations in Integers
Corresponding bits of both operands are combined by the
usual logic operations.

• & – AND • ~ – Complement


• Result is 1 if both • Each bit is reversed
operand bits are 1
• | – OR • << – Shift left
• Result is 1 if either • Multiply by 2
operand bit is 1
• ^ – Exclusive OR • >> – Shift right
• Result is 1 if operand
• Divide by 2
bits are different
Apply to all kinds of integer types:–
Signed and unsigned
char,& Bitwise
Bit Fields short, int, long, long long
3
Operations
Summary:Bitwise Operators

& bitwise AND


| bitwise OR
^ bitwise XOR
~ 1’s compliment
<< Shift left
>> Shift right

All these operators can be suffixed with =


For instance a &= b; is the same as a = a & b;
4
Bitwise Operators -
Examples
11010011 11010011 11010011
& | ^
10001100 10001100 10001100
------------ ------------ ------------
10000000 11011111 01011111

~11010011 11010011>>3 11010011<<3


------------ ------------ ------------
00101100 00011010 10011000

5
Other Bitwise Operators

• Examples:
unsigned short int i, j, k;
i = 21; /* i is now 21 (binary 0000000000010101) */
j = 56; /* j is now 56 (binary 0000000000111000) */
k = ~i; /* k is now 65514 (binary 1111111111101010) */
k = i & j; /* k is now 16 (binary 0000000000010000) */
k = i ^ j; /* k is now 45 (binary 0000000000101101) */
k = i | j; /* k is now 61 (binary 0000000000111101) */

• Warning: Don’t confuse the bitwise operators & and | with


the logical operators && and ||.
Examples
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
#include <stdio.h>
int main() Bitwise AND
{ 00001000 = 8 (In decimal)

int a = 12, b = 25; Bitwise OR


printf("Output = %d", a&b); 00011101 = 29 (In decimal)

Bitwise XOR
printf("Output = %d", a|b); 00010101 = 21 (In decimal)

printf("Output = %d", a^b); complement=-36

Complement=11
printf("complement=%d\n",~35);

printf("complement=%d\n",~-12);

return 0; Bit Fields & Bitwise 7


Operations
}
Examples i=0 1100 = 12
#include <stdio.h> i=1 0110 = 6
int main() i=2 0011 = 3
{
int num=12, i; i=0 1100 = 12
i=1 0001 1000 = 24
for (i=0; i<=2; i++) i=2 0011 0000 = 48
printf("Right shift by %d: %d\n", i, num>>i); Right Shift by 0: 12
Right Shift by 1: 6
printf("\n");
Right Shift by 2: 3

for (i=0; i<=2; i++)


printf("Left shift by %d: %d\n", i, num<<i); Left Shift by 0: 12
Left Shift by 1: 24
return 0;
} Left Shift by 2: 48

Bit Fields & Bitwise 8


Operations
Setting Bits
 How can we set a bit on or off?
 Manipulations on bits are enabled by
mask and bitwise operators.
 Bitwise OR of anything with 1 results
in 1.
 Bitwise AND of anything with 0
results in 0.
9
Setting Bits
 For instance, how can we turn on
the light in room #3?
lights: 00000000

char lights = 0x0; mask: 00000001


char mask = 0x1;
mask <<= 2;
lights |= mask; mask: 00000100

lights: 00000100
10
Setting Bits
 For instance, how can we turn off
the light in room #3?
lights: 00100111

char lights = 0x27; mask: 11111011


char mask = 0xfb;
lights &= mask;

lights: 00100011
11
Getting Bits
 How can we know if a bit is on
or off?
 Manipulations
on bits are
enabled by mask and bitwise
operators.
 Bitwise AND of anything with 1
results in the same value.
12
Getting Bits
 For instance, how can we check if
the light in room #3 is turned on or
off?
lights: 00100111
char lights = 0x27;
char mask = 0x1;
mask <<= 2; mask: 00000001
if(lights & mask)
mask: 00000100
puts(“turned on”);
else
puts(“turned off”); lights & mask: 00000100

13
Questions-1

Write a program to print Binary


representation of a given number.

Bit Fields & Bitwise 14


Operations
Answer-1
unsigned x,m;
int k;
scanf("%d",&x);
printf("\nx=%x and x=%d\n\n",x,x);
printf("\n\n");

m=0x0080;
for(k=8;k>0;k--)
{ X=36
if(x&m)
{ printf("1"); } X=24 (0x24=16*2+1*4) and X=36
else
{ printf("0"); }
00100100
m=m>>1;
}

Bit Fields & Bitwise 15


Operations
Question-2
• Write a program to convert Binary number to Decimal number.
• Ask to the user to enter any number in binary.
• Convert the given binary number into decimal form.
• Use while loop.
• Display the result on the screen like this:

Bit Fields & Bitwise 16


Operations
Answer-2
int main(int argc, char *argv[]) {
int binnum, decnum=0, i=1, rem;
printf("Enter A Number in Binary : ");
scanf("%d",&binnum);

while(binnum!=0)
{
rem=binnum%10;
decnum=decnum+rem*i;
i=i*2;
binnum=binnum/10;
}

printf("Decimal Form = %d",decnum);


return 0; Bit Fields & Bitwise
Operations
17

}
Alternative Way For Question-2
int main(int argc, char *argv[]) {

long binnum,sum=0,i=0;
printf("Enter A binary Number=");
scanf("%ld",&binnum);

while(binnum!=0)
{
if(binnum%10==1) {sum=sum+pow(2,i);}
binnum=binnum/10;
i++;
}
printf("The Decimal form =%d",sum);
return 0;
}
Bit Fields & Bitwise 18
Operations
Question-3
• What is the output of the given program in C
int main(int argc, char *argv[]) {
int num, count, ms, i, bit;
bit=8; num=6; count = 0;
ms = 1 << (bit - 1);
for(i=0; i<bit; i++)
{
if((num << i) & ms) break;
count++;
}
printf("Total number in %d is %d", num, count);
return 0;
} Bit Fields & Bitwise 19
Operations
Example for Final Exam
Write a C program to convert decimal to binary number and count
trailing zeros in a binary number for 8 bits.
• Ask to the user to enter any number in decimal form.
• You must NOT use any bitwise operators for converting and
displaying the binary form.
• You must use bitwise the operators for finding trailing zeros in
binary number.
• You must use only for loop.
• Display the result on the screen like this:

20
Answer for Final Exam
int main(int argc, char *argv[]) {
int arr[50]={0},arrbin[50]={0};
int num,numbin, count=0, i;
printf("Enter any number: "); scanf("%d", &num);
numbin=num;
for(i=7;i>=0;i--)
{ 0 1 2 3 4 5 6 7
arr[i]=numbin%2; 0 0 0 1 1 1 1 0
numbin=numbin/2;
}

printf("The Binary Code= "); for(i=0;i<8;i++){printf("%d ",arr[i]);}

for(i=0; i<8; i++)

if((num >> i ) & 1) { break; }


count++;
} if((num >> i=3 ) & 1) TURE

printf("\nTotal number of trailing zeros is %d", count);


return 0; 21
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy