Bit Manipulation. Get Bit from the particular Position Suppose you want to get a bit from the 4 th position. You can call int get_bit_val(int number_send,int.

Slides:



Advertisements
Similar presentations
Getting Input in Python Assumes assignment statement, typecasts.
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Fraction class class fraction { int num, den; fraction (int a, int b){ int common = gcd(a,b); num=a/common; den=b/common; } fraction (int a){ num=a; den=1;
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
Operations on Arrays. Operation on Array Data Structures  Traversal  Selection  Searching  Insertion  Deletion  Sorting.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Programming Functions: Passing Parameters by Reference.
Passing Arguments Question Example: IS1102 Exam Autumn 2001.
Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
Bitwise Structures Chapter 9: C programming Language ספטמבר 04
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
CMPE13 Cyrus Bazeghi Chapter 17 Recursion. CMPE13 What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
CMSC 1041 Functions II Functions that return a value.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Bitwise Operators Fall 2008 Dr. David A. Gaitros
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Creating Web Services Presented by Ashraf Memon Presented by Ashraf Memon.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
User-Defined Functions (cont’d) - Reference Parameters.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
User Interaction and Variables
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Programming in Machine Language
Lecture-5 Arrays.
Arrays Part-1 Armen Keshishian.
Introduction to Programming and the C Language
Chapter 17 Recursion.
Chapter 5 Function Basics
Andy Wang Object Oriented Programming in C++ COP 3330
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Assignment Operators Topics Increment and Decrement Operators
FUNCTION CSC128.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Dry Run Practice Use Methods with an Insertion Sort
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers & Functions.
Bitwise Operators.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Bit Manipulations CS212.
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

Bit Manipulation

Get Bit from the particular Position Suppose you want to get a bit from the 4 th position. You can call int get_bit_val(int number_send,int position) function. In this function, number_send is our data from where we want to get a bit. Second pass the position from where you want to get a bit.

Get Bit function int get_bit_val(int number_send,int position) { int number1=1; number1=number1<<(position-1); if(number_send & number1) { return (1); } else { return (0); }

How Get bit function works? First argument we can pass as int. so ascii value is passed there as a number_send. Suppose user has entered a character N that you pass as a argument to get bit function. Ascii of N is 78 that is Second argument you pass for the 4 th position bit.

Contd. In the first step number1 =1 is initialized. Number_send Number1 Number1<<(position-1) : Number1<<3 Number_send Number

if((number_send & number1)) return (1); else return (0); Number_send Number1 It will generate answer 0 or 1 as per the number_send value. In this case, it will generate 1. Contd &

Set Bit function Through this function, we can set the bit with our own value on the particular position. The function void setbit(int *data, int pos, int val) takes 3 argument one is data, second is position and third is the value which we want to set on the particular position in the data.

Set Bit Function void setbit(int *data, int pos, int val) { int mask=1; mask = mask<<(pos-1); if(val==0) { mask=~mask; *data=*data & mask; } else { *data=*data | mask; }

How set bit function works? First one variable mask is initialized with one then after it is shifted on the particular position. Then if value which is passed as third argument, if it is 0 then mask is complemented. So, if we perform data & mask then it sets 0 on that position. If value which we want to set is 1 then data is ored with 1 so it sets 1 on that position.

Example In the set bit function suppose arguments are for data N, position is 4 and new value what we want to set is 0. Initially it is Data Mask After performing left shift for mask Data Mask

if(val==0) { mask=~mask; *data=*data & mask; } In our case value is 0, means we want to set 0 on 4 th position. mask ~mask Then last operation is data & mask Example & =

Insert Bit int ins_bit_pos(int number_send,int position,int value) { int num1,num2; num1=number_send; num1=num1>>(position-1); num1=num1<<(position-1); num2=number_send-num1; num1=num1<<1; num1=num1 | num2; if(value==1) { num1=set_bit_val(num1,position,value); } return (num1); }

We want to add bit 0 on the 3rd position. Suppose number_send is N. which is assigned to num1 in the first step. So, Num1 Num1>> (position-1) Num1<<(position-1) Num2 = number_send – num =

num1= num1<<1 num1= Num1<<1 num1 = num1|num2 Bit 0 is set on the 3 rd Position | =