Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Bitwise Operations

• 'C' was designed to write system software as an alternative to assembler:


compilers, kernels, device drivers, interpreters, relational database engines,
virtual machines.

• So this language needs access to raw hardware and individual bit values.
Coding designed for specific hardware design features will be non portable.

• Different microprocessors organise integer and floating point data differently,


e.g. two's or one's complement, location and size of exponent, sign bit and
mantissa.

• Device drivers for different hardware implement different instruction sets.

• Many situation, need to operate on the bits of a data word –


• Register inputs or outputs
• Controlling attached devices
• Obtaining status
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
• Result is 1 if either
<< – Shift left
• Multiply by 2
operand bit is 1
^ – Exclusive OR
• Result is 1 if operand >> – Shift right
bits are different • Divide by 2
Left and Right Shift Operators
The >> operator shifts a variable to the right and the
<< operator shifts a variable to the left. Zeros are shifted
into vacated bits, but with signed data types, what
happens with sign bits is platform dependant.

The number of bit positions these operators shift the


value on their left is specified on the right of the operator.

Uses include fast multiplication or division of integers by


integer powers of 2, e.g. 2,4,8,16 etc.
Left and right shift example

#include <stdio.h>
int main(void){
unsigned int a=16;
printf("%d\t",a>>3); /* prints 16 divided by 8 */
printf("%d\n",a<<3); /* prints 16 multiplied by 8 */
return 0;
}

output: 2 128
Bitwise AND and inclusive OR

Single & and | operators (bitwise AND and OR)


work differently from logical AND and OR
( && and || ). You can think of the logical operators
as returning a single 1 for true, and 0 for false.

The purpose of the & and | bitwise operators is to


return a resulting set of output 1s and 0s based on
the boolean AND or OR operations between
corresponding bits of the input.
Bitwise AND and inclusive OR
Truth Table Bitwise AND (&) Truth Table Bitwise OR (|)

Input Output Input Output


A B C A B C
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1

Example: a=28, b=0 Example: a=28, b=32


A 0 0 0 1 1 1 0 0 A 0 0 0 1 1 1 0 0
& B 0 0 0 0 0 0 0 0 | B 0 0 1 0 0 0 0 0
C 0 0 0 0 0 0 0 0 C 0 0 1 1 1 1 0 0
Example: a=28, b=255 Example: a=28, b=29
A 0 0 0 1 1 1 0 0 A 0 0 0 1 1 1 0 0
& B 1 1 1 1 1 1 1 1 | B 0 0 0 0 0 0 0 1
C 0 0 0 1 1 1 0 0 C 0 0 0 1 1 1 0 1
Bitwise AND/OR example
#include <stdio.h>
int main(void){
unsigned char a='\x00',b='\xff',c;
c='\x50' | '\x07'; /* 01010000 | 00000111 */
printf("hex 50 | 07 is %x\n",c);
c='\x73' & '\x37'; /* 01110011 & 00110111 */
printf("hex 73 & 37 is %x\n",c);
return 0;
}

Output:
hex 50 | 07 is 57
hex 73 & 37 is 33
Bitwise exclusive OR operator

Symbol: ^

For each bit of output, this output is a 1 if


corresponding bits of input are different, and the
output is a 0 if the input bits are the same.
One's complement operator

Symbol: ~

This is a unary operator in the sense that it works


on a single input value. The bit pattern output is
the opposite of the bit pattern input ­with input 1s
becoming output 0s and input 0s becoming output
1s.
Setting a particular bit within a byte

Prototype used:

void setbitn(unsigned char *cp, int bitpos, int value);


/* setbitn sets bit position 0 ­7 of cp to 0 or 1 */

The byte cp (assuming chars are 1 byte wide) is


passed by reference.
Setting a particular bit within a byte
void setbitn(unsigned char *cp,int bitpos,int value){
/* setbitn sets bit position 0 ­7 of cp to value 0 or 1 */
unsigned char template=(unsigned char)1;
/* first make template containing just the bit to set */
template<<=bitpos;
if(value) /* true if value is 1 false for 0.
Bitwise OR sets templated bit in cp to 1 whatever
its current value, leave other bits unchanged */
*cp=*cp | template;
else
/* Invert template 1s and 0s. Use bitwise AND to
force templated bit in cp to 0
* and leave all other bits in cp unchanged */
*cp=*cp & ( ~ template);
}
Getting a particular bit within a byte

To return the value of a particular bit within a byte


without changing the original, the following
prototype was used:

int getbitn(unsigned char c, int bitpos);


/* getbitn gets bit position 0 ­7 of c, returns 0 or 1 */

Call by value is used for the byte concerned, so this


can be changed within the function, but as this is a
copy the original byte won't be changed.
Getting a particular bit within a byte

int getbitn(unsigned char c, int bitpos){


/*getbitn gets bit position 0 ­7 of c, returns value 0 or 1.
This function writes to c, but as we are using pass by
value, this won't affect the original calling copy. */

unsigned char template=(unsigned char)1;


/* make template containing just the bit to get */
template<<=bitpos;
c&=template;
/* if relevant bit set then c is assigned non null,
otherwise c is assigned null (all zeros) */
if(c) return 1;
else return 0;
}
Examples
a 1 1 1 1 0 0 0 0

b 1 0 1 0 1 0 1 0
unsigned int c, a, b;
c = a & b;
c = a | b;
c = a ^ b;
c = ~a;

c = a << 2;
c = a >> 3;
Right Shift
unsigned int c, a; a 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0

c = a >> 3;

signed int c, a, b; b 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
c = b >> 3;
c = a >> 3;
a 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0
Passing Command Line Arguments
• When you execute a program you C:\>try –g 2 fred
can include arguments on the
command line.
• The run time environment will argc = 4,
argv = <address0>
create an argument vector.
– argv is the argument vector
– argc is the number of
arguments
‘t’‘r’‘y’‘\0’
• Argument vector is an array of argv:
pointers to strings. [0] <addres1> ‘-’‘g’‘\0’
[1] <addres2>
• a string is an array of characters [2] <addres3>
terminated by a binary 0 (NULL or [3] <addres4> ‘2’‘\0’
[4] NULL
‘\0’).
• argv[0] is always the program ‘f’‘r’‘e’‘d’‘\0’
name, so argc is at least 1.
Passing Command Line Arguments
/* Example Program “add.c” */ C:\>add 10 5
#include <stdio.h>
#include <stdlib.h>
argc = 3,
#include <conio.h> argv = <address0>

int main(int argc, char *argv[])


{
int a, b; ‘a’‘d’‘d’‘\0’
argv:
a=(atoi)(argv[1]); [0] <addres1> ‘1’‘0’‘\0’
b=(atoi)(argv[2]); [1] <addres2>
[2] <addres3>
printf("\n\n\t\t\t%d\n\n", a+b); [3] NULL ‘5’‘\0’
getch();
return 0;
}
Passing Command Line Arguments
/* Example Program C:\>filecopy srcfile.c targfile.c
“filecopy.c” */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(int argc, char


*argv[])
{
FILE *fs, *ft;
char ch;
……….
}

You might also like