Sunteți pe pagina 1din 50

Bitwise Operations

ESC101
April 6, 2018
Mid-Sem Lab Exam: Date and Time
• Lab end sem on April 8 (Sunday)
Lab Sections Time
CC-02 A9, A10, A11 9:00 am – 12:00 am
CC-01 A7 9:00 am – 12:00 am
CC-03 A8 9:00 am – 12:00 am
Maths Lab (NCL) A12, A13 9:00 am – 12:00 am

Lab Sections Time


CC-02 A1, A2, A3 2:00pm – 5:00 pm
CC-01 A4 2:00pm – 5:00 pm
CC-03 A5 2:00pm – 5:00 pm
Maths Lab (NCL) A6 2:00pm – 5:00 pm
End-Sem Lab Exam: Syllabus
• Up to Lecture 29 (Linked List)
Announcement: Informal Lab Session
• April 6 (Friday) from 2pm to 6pm in NCL CC-02

• The lab will not be graded


Tips for Lab Exam
• Read all instructions and the questions carefully.
• Approaching a problem:
Don't try to write the entire program in one attempt.
Write short segments of code and keep compiling
whenever possible to ensure correctness.
Use functions to divide your program into smaller
components.
Write functions to perform ONE task at a time.
Combine the tasks in main.

Apr-18 5 ESC101 Midsem/Lab Exam


Tips for Lab Exam
• If your program is not working
insert dummy printf statements to test where it is going
wrong.
 Specially test input/output of the functions you write.
make a variable table (especially when using loops).
Debug ONE iteration of a loop at a time.
• Important: Write comments to explain your code.
• Give meaningful variable names.
• Use proper indentation.
• Do not rely solely on the test cases provided by us.
Apr-18 6 ESC101 Midsem/Lab Exam
Conduct for Lab Exam
• Lab exam will be conducted through
esc101.cse.iitk.ac.in
• The network will be ON during the exam
• Closed book exam
– You are not allowed to access any other site during the
exam
– Do not check emails (@iitk, @gmail, …)
– Mobiles are not allowed on person (even if switched off)
• ITS stores your keystrokes and program
development steps!
Apr-18 7 ESC101 Midsem/Lab Exam
Conduct for Lab Exam
• We will monitor system usage
– Tracking IP used for submission and incoming
and outgoing network traffic
– Manually and automatically
• Please don’t cheat
– It’ll make life unpleasant for all concerned
– One student is getting an F in the course for
copying on the lab midsem exam

Apr-18 8 ESC101 Midsem/Lab Exam


Theory End-Sem Exam
• April 26, 9 am to 12 noon
• Closed Book Exam
• Bring your ID card
• Don’t bring your mobile phone
Theory End-Sem Exam
Room Sections
L16 A1, A2
L17 A5, A6
L18 A7, A8, A13
L19 A9, A10, A13
L20 A3, A4, A11, A12

• Seating arrangement : even row even seat


(ERES)
• First even rows of L18 and L19 are reserved for
students of Section A13
Tips for End Sem Exam
• Questions where you need to complete a
partially filled program:
 First understand the problem.
 Then try to understand the given code.
 Complete the program.
 Check whether the completed program is
behaving as it should.
• Finally check whether you have answered all
questions and verify your answers.
• Practice!
Apr-18 11 ESC101 Midsem/Lab Exam
Syllabus
• Everything that has been and will be covered
in this course
Today
• Register Storage Class
• Bitwise operator
• Shell Programming

• More about Computer Science


Register Storage Class
• You have seen the following storage classes:
– Atomatic, external, static
• The other storage class is register storage class
– register int a,b, c
• Usage rules are same as automatic variables
• Address operator can not be applied to register
variables
• Are not guaranteed to be treated as register
variables
Bitwise Operators
Operators Meaning of Operator

& Bitwise And


| Bitwise Or
^ Bitwise Xor
~ Bitwise Complement
<< Shift Left
>> Shift Right
Bitwise And Operator &
• The output of bitwise AND is 1 if the
corresponding bits of two operands is 1
• If either bit of an operand is 0, the result of
corresponding bit is evaluated to 0

12 = 00001100 (In Binary) #include <stdio.h>


25 = 00011001 (In Binary)
int main()
Bit Operation of 12 and 25 {
00001100 int a = 12, b = 25;
&00011001 printf("Output = %d", a&b);
________ return 0;
00001000 = 8 (In decimal) }
Bitwise OR Operator |
• The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1
• In C Programming, bitwise OR operator is
denoted by |

12 = 00001100 (In Binary) #include <stdio.h>


25 = 00011001 (In Binary)
int main()
Bitwise OR Operation of 12 and 25 {
00001100 int a = 12, b = 25;
| 00011001 printf("Output = %d", a|b);
________ return 0;
00011101 = 29 (In decimal) }
Bitwise XOR Operator ^
• The result of bitwise XOR operator is 1 if the
corresponding bits of two operands are opposite
• It is denoted by ^

12 = 00001100 (In Binary) #include <stdio.h>


25 = 00011001 (In Binary)
int main()
Bitwise XOR Operation of 12 and {
25 int a = 12, b = 25;
00001100 printf("Output = %d", a^b);
^ 00011001 return 0;
________ }
00010101 = 21 (In decimal)
Bitwise Complement Operator ~
• Bitwise compliment operator is an unary
operator (works on only one operand).
• It changes 1 to 0 and 0 to 1.
• It is denoted by ~.

35 = 00100011 (In Binary)

Bitwise complement Operation of


35
~ 00100011
________
11011100 = 220 (In decimal).
Bitwise Complement Operator ~
in C Programming Language

• The bitwise complement of 35 (~35) is -36 instead of


220, but why?

• For any integer n, bitwise complement of n will be


-(n+1).

• To understand this, you should have the knowledge


of 2's complement.
2’s Complement
• Two's complement is an operation on binary
numbers. The 2's complement of a number is
equal to the complement of that number plus
1.
Decimal Binary 2's complement
0 00000000 -(11111111+1) = -00000000 = -0(decimal)
1 00000001 -(11111110+1) = -11111111 = -256(decimal)
12 00001100 -(11110011+1) = -11110100 = -244(decimal)
220 11011100 -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.


Bitwise Complement of a Number
• bitwise complement of N = ~N (represented in
2's complement form)
• 2'complement of ~N= -(~(~N)+1) = -(N+1)
Right Shift Operator >>
• Right shift operator shifts all bits towards right
by certain number of specified bits.
• It is denoted by >>.

212 = 11010100 (In binary)


212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)
Left Shift Operator <<
• Left shift operator shifts all bits towards left by
certain number of specified bits.
• It is denoted by <<.

212 = 11010100 (In binary)


212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 =11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)
Shift Operator Example
#include <stdio.h>
int main()
{
int num=212, i;
for (i=0; i<=2; ++i)
printf("Right shift by %d: %d\n", i, num>>i);

printf("\n");

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


printf("Left shift by %d: %d\n", i, num<<i);

return 0;
}
Operator Precedence
Operators Description Associativity
( ) [] -> . Left to Right
(unary)- ++ -- ! ~ Unary plus/minus etc., logical Not Right to left
(type) * & sizeof
HIGH */% Multiply, divide, remainder Left to right
+- Add, subtract Left to right
I
N
C >> << Shift Operators Left to right
R
E
A < > >= <= Relational operators Left to right
S
I
N
== != Equal, not equal Left to right
G
& Bitwise and Left to right
^ Bitwise xor Left to right
| Bitwise or Left to right
&& And Left to right

LOW
|| Or Left to right
?: Conditional Right to left
= += -= &= |=
4/18/2018 Assignment 26 Right
Esc101,to left
Programming
Shell Programming
• A shell program (sometimes called a shell
script) is a text file that contains standard
UNIX and shell commands.

• Useful for automation


Shell Program Example
#include<stdio.h>
prog1.c
int main() {
return 4;
}

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){ prog2.c

return atoi(argv[1]) * 5;
}
Shell Program Example
#!/bin/sh
# This is a comment!

gcc -o out1 prog1.c


gcc -o out2 prog2.c
./out1
./out2 $?
echo $?

Output: 20
Running a shell script
$ vi run.sh

$ Chmod +x run.sh

$ ./run.sh
20
The Story has just Started
• Systems
– Operating system
– Networks
– Software design
– Security
• Theory
– Algorithms
– Complexity
• Applications
– Data mining
– Machine learning
– Computational science
– Game design

Apr-18 Esc101,FileIO 31
Operating Systems
• What does an OS do?
– Talk to devices
– Controls programs
– Manages data access
– Manages resource access
– Manages network communication
• Linux kernel written in C
– You can create your own OS by adding kernel modules
to pre-existing base OS
• Try it on a PC you don’t mind wrecking!
Security
• Reading from and writing to unknown parties
is risky
• Particularly when information is financial
• Have to secure access
• This is a job for cryptography
• Basic idea is to encrypt message such that
only entities that should be receiving the
information can decrypt
Theory
• Algorithm design tries to address issues of
efficiency and scale
• We will see some examples, such as in merge-
sort and quick-sort
• Vast area, with massive multiplier influence
Data analysis
Working with Data
• When we know what we’re looking for
– Data analysis
– Targeted, high quality data
– Domain knowledge matters
• When we don’t know what we’re looking for
– Data mining
– Exploratory, data quality unknown
– Algorithmic prowess and skill matters
Machine Learning
• Subset of data mining
• Basic idea is to
– Get algorithms to find things that look like other
things we show them
– Get algorithms to categorize things automatically
• Lots of very well-developed code libraries pre-
exist
– Weka
– Matlab ML toolbox
– Scikit-learn library in python
• You want to know python
Possibilities are Wide Open
• Have fun writing code the rest of your time
here and beyond
• Don’t hesitate to talk to any of the CSE faculty
– It would help if you know what you want to talk
about
• Be bold in coming up with ideas
– Worst case scenario: it won’t work
– Your next idea will be a better one, guaranteed
Remaining Classes
Lectures on Sorting
• Prof. Purushottam kar (Faculty Tutor)
Assistant Professor
Department of CSE

• Dates: April 9, 11, 13


Lectures on
Introduction to Python Programming
• Prof. Swaprava Nath
Assistant Professor
Department of CSE

• Dates: April 16, 18, 20


Remaining Tutorials
• April 10: Discussion and Minor Quiz on
– Command line Argument
– File Handling

• April 17: Discussion and Minor Quiz (may be)


on
– Sorting
Remaining Labs
• Week of April 9-12:
– Command line Argument
– File Handling

• Week of April 16-19


– Sorting
Acknowledgement
Software and Slides

• Prutor
• Gradescope
• Piazza

• Slides
– From former instructors, Profs Nisheeth
Srivastava, Rajat Mittal, Nitin Saxena, Sunil Simon,
Vinay Nambudoori, Amey Karkare and others
Apr-18 Esc101,FileIO 45
Team
• Faculty Tutor Prof. Purushottam Kar

• 45 TAs

• 18 Tutors

• Lab staffs

• Lecture Hall staffs

• Other CSE faculty members helped in invigilation of theory


exams
Apr-18 Esc101,FileIO 46
Section Tutors

• Srishti (A1) • Govind (A8)


• Afroz (A2) • Atul (A9)
• Kunal (A3) • Utsab (A10)
• Sanjay (A4) • Rohit (A11)
• Nishit (A5) • Akash (A12)
• Dhawal (A6) • Anuj (A13)
• Rishav (A7) • Anil
• Aravind
Apr-18 Esc101,FileIO 47
Administrative Tutors

• Hrishikesh

• Shubhangi

• Umair

Apr-18 Esc101,FileIO 48
Course Feedback
• OARS section will send you a link to the course
feedback form
• You will be able to give feedback about the
course, the instructor and the tutors
• Please do
Have fun writing code for the rest of your time
here and beyond…

S-ar putea să vă placă și