Bitwise Operations
Bitwise Operations
Bitwise Operations
• So this language needs access to raw hardware and individual bit values.
Coding designed for specific hardware design features will be non portable.
#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
Output:
hex 50 | 07 is 57
hex 73 & 37 is 33
Bitwise exclusive OR operator
Symbol: ^
Symbol: ~
Prototype used:
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>