You are on page 1of 49

CSD101: Introduction to

Computing and Programming


Lecture 19
Today’s Lecture…

• Bit-wise Operators
• One’s Complement Operator
• Right Shift Operator
• Left Shift Operator
• Bit-wise AND Operator
• Bit-wise OR Operator
• Bit-wise XOR Operator
• Bit-wise Compound Assignment Operator
• Comma Operator
• Command Line Arguments
Bit and Bytes…

• Bit: Binary Digit (values: 0 or 1)


• Nibble: Group of 4-bits
• Byte: Group of 8-bits
• Word: Group of 16-bits
• Double-word: Group of 32-bits
Bit Numbering
Operators

Note: Only defined for int and char values, not for float or double
One’s complement Operator (~)
Example: One’s complement
Example: One’s complement
Right Shift Operator (>>)
Right Shift Operator (>>)
Left Shift Operator (<<)
Use of ‘<<’ to set 1 to a particular bit-position
Use of ‘<<’ to set 1 to a particular bit-position
Use of ‘<<’ to set 1 to a particular bit-position
Use of ‘<<’ to set 1 to a particular bit-position
Bit-wise AND Operator (&)
Bit-wise AND Operator (&)
Use of AND Operator

• To check if a particular bit of an operand is ON (i.e.,1) or OFF (i.e.,0)


• To turn OFF a particular bit
Use of AND Operator

• To check if a particular bit of an operand is ON (i.e.,1) or OFF (i.e.,0)


• To turn OFF a particular bit
• Example: For a given bit pattern 10101101 (operand 1), check if bit
number 5 is ON (1) or OFF (0).
Use of AND Operator
Use of AND Operator
Use of AND Operator
Use of AND Operator: To turn OFF a particular bit

• Example: For a given bit pattern 10101101 (operand 1), set the 3rd
position to the value 0 means turn the 3rd bit OFF.
Use of AND Operator: To turn OFF a particular bit

• Example: For a given bit pattern 10101101 (operand 1), set the
3rdposition to the value 0 means turn the 3rdbit OFF.
• Take second operand with bit number 3 as 0
• 11110111 (operand 2)
Use of AND Operator: To turn OFF a particular bit

• Example: For a given bit pattern 10101101 (operand 1), set the
3rdposition to the value 0 means turn the 3rdbit OFF.
• Take second operand with bit number 3 as 0
• 11110111 (operand 2)
Bit-wise OR Operator (|)
Bit-wise OR Operator (|)
Use of OR Operator: To turn ON a particular bit
Bit-wise XOR Operator (^)
Bit-wise XOR Operator (^)
Example: Function to Print Binary Equivalent of Integers
Example: Function to Print Binary Equivalent of Integers
Example: Function to Print Binary Equivalent of Integers
Example: Function to Print Binary Equivalent of Integers
Example: Function to Print Binary Equivalent of Integers
Example: Bit-wise Operators
Example: Bit-wise Operators
Bit-wise Assignment Operator
Comma (,) Operator
• A pair of expressions separated by a comma is evaluated left-to-right,
and the value of the left expression is discarded.
• The type and value of the result are the type and value of the right
operand.
• Comma operator appears only in a parenthetical grouping.
• Example:
f(a, (t=3, t+2), c)
has three arguments, the second of which has the value 5.
Precedence Table
Example
Example
Command-line Arguments
Command-line Arguments
• We can pass command-line arguments or parameters to a program when it
begins executing.
• We can pass two arguments to main function.
main(int argc, char *argv[])
• The first (called argc, for argument count) is the number of argument in the
command-line;
• The second (argv, for argument vector) is a pointer to an array of character strings
that contain the arguments, one per string.
• argv[0] is the name of the program
• argv[1] is the first argument,
• argv[2] is the second and so on till argv[argc-1]
Example
Example
Example
Example

You might also like