SlideShare a Scribd company logo
1 of 25
MOHAN LAL SUKHADIYA
UNIVERSITY
University Computer Centre
Submitted To: Sanjiv Agrawal Sir
Submitted By: Priyanka Yadav(MCA 3rd
sem)
Topic: BIT
MANIPULATION
IN JAVA
Contents:
• As is well known, numbers are stored and operated as
binaries inside a computer. So, bit manipulation, as named,
is to manipulate binary bits of a number directly.
E.g. 6 & 11 = 0x110 & 0x1011 = 0x0001 = 2
• Java provides extensive bit-manipulation capabilities for
programmers who need to get down to the “bits-and-bytes”
level.
• Computers represent all data internally as sequences of
bits. Each bit can assume the value 0 or the value 1.
Bit Manipulation??
Bitwise Operators
Bitwise operators perform operations
on the bits of their operands. The
operands can only be byte, short, int,
long or char data types.
For example, if an operand is the
number 48, the bitwise operator will
perform its operation on the binary
representation of 48 (i.e., 110000).
Bitwise AND(&):
-set to 1 if both bits are 1.
Bitwise OR(|):
-set to 1 if atleast one bit is 1.
Bitwise exclusive OR:
-set to one if only 1 bit is set to 1.
Bitwise ~:
-All 1’s become 0 ‘s and all 0’s become 1’s.
The left shift operator will shift the bits towards left for the
given number of times.
int a=2<<1;
Let’s take the binary representation of 2 assuming int is 1
byte for simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 0 1 0
Now shifting the bits towards left for 1 time, will give the
following result
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Left Shift <<:NOTE:
Now the
result in
decimal is 4.
If you left
shift like
2<<2, then it
will give the
result as 8.
Therefore
left shifting 1
time, is
equal to
multiplying
the value by
2.
The right shift operator will shift the bits towards right for the given
number of times.
int a=8>>1;
Let’s take the binary representation of 8 assuming int is 1 byte for
simplicity.
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 1 0 0 0
Now shifting the bits towards right for 1 time, will give the following
results
Position 7 6 5 4 3 2 1 0
Bits 0 0 0 0 0 1 0 0
Right Shift >>:
Now the
result in
decimal is
4. Right
shifting 1
time, is
equivalent
to dividing
the value
by 2.
NOTE:
Uses Of Bitwise
Operators:
Bitset class
BITSET CONSTRUCTOR:
Sno Constructor Description
1. BitSet( ) This constructor creates a new bit
set.
2. BitSet(int size) This constructor allows you to
specify its initial size, i.e., the
number of bits that it can hold. All
bits are initialized to zero
Sno Method Description
1. void and(BitSet set) This method performs a logical AND of this target
bit set with the argument bit set.
2. void andNot(BitSet
set)
This method clears all of the bits in this BitSet
whose corresponding bit is set in the specified
BitSet.
3. int cardinality( ) This method returns the number of bits set to true
in this BitSet.
4. void clear( ) Zeros all bits.
5. void clear(int index) Zeros the bit specified by index.
BITSET METHODS:
6. void set(int bitIndex) Sets the bit specified by index to true.
7. void set(int bitIndex,
boolean v)
This method sets the bit at the specified index to
the specified value.
8. void set(int startIndex, int
endIndex)
Sets the bits from(inclusive) startIndex to
endIndex (exclusive)to true.
9. String toString( ) Returns the string equivalent of the bit set.
10. void xor(BitSet bitSet) This method performs a logical XOR of this bit
set with the bit set argument.
11. Object clone( ) This method clones the BitSet and
produces a new BitSet that is equal to it.
12. int hashCode() This method returns a hash code value
for this bit set.
13. void flip(int index) method sets the bit at the specified index
to the complement of its current value.
14. int length() This method returns the "logical size" of
this BitSet: the index of the highest set bit
in the BitSet plus one.
15. boolean get(int index) This method returns the value of the bit
with the specified index.
16. boolean intersects(BitSet
set)
This method returns true if the specified
BitSet has any bits set to true that are
also set to true in this BitSet.
import java.util.BitSet;
public class BitSetDemo
{
public static void main(String args[])
{
BitSet bitset1=new BitSet(8);
BitSet bitset2=new BitSet(8);
// assign values to bitset1
bitset1.set(0);
bitset1.set(1);
bitset1.set(2);
bitset1.set(3);
bitset1.set(4);
bitset1.set(5);
// assign values to bitset2
bitset2.set(2);
bitset2.set(4);
bitset2.set(6);
bitset2.set(8);
bitset2.set(10);
System.out.println("Bitset1:" + bitset1);
System.out.println("Bitset2:" + bitset2);
bitset1.andNot(bitset2);
System.out.println("" + bitset1);
System.out.println("" +bitset1.cardinality());
System.out.println("" + bitset1.toString());
System.out.println("" + bitset2.toString());
System.out.println("Bitset1:" + bitset1)
bitset2 = bitset1.clone();
System.out.println("" + bitset1.hashCode());
System.out.println("" + bitset1.length());
System.out.println("" + bitset1.intersects(bitset2));
}
}
OUTPUT:
Bitset1: 0 1 2 3 4 5
Bitset2: 2 4 6 8 10
{0, 1, 3, 5}//and not
6//cardinality
{0, 1, 2, 3, 4, 5}//string
{2, 4, 6, 8, 10}
0 1 2 3 4 5 //clone
1261//hash code
6//length
true //intersects
SIEVE OF ERATOSTHENES
TECHNIQUE:
OUTPUT:
Bit manipulation

More Related Content

What's hot (20)

Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Get started python programming part 1
Get started python programming   part 1Get started python programming   part 1
Get started python programming part 1
 
List in Python
List in PythonList in Python
List in Python
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Data visualization in Python
Data visualization in PythonData visualization in Python
Data visualization in Python
 

Similar to Bit manipulation

C bitwise operators
C bitwise operatorsC bitwise operators
C bitwise operatorsSuneel Dogra
 
Bitwise Operations(1).pdf
Bitwise Operations(1).pdfBitwise Operations(1).pdf
Bitwise Operations(1).pdfDalvinCalvin
 
Cse lecture-4.2-c bit wise operators and expression
Cse lecture-4.2-c bit wise operators and expressionCse lecture-4.2-c bit wise operators and expression
Cse lecture-4.2-c bit wise operators and expressionFarshidKhan
 
Lesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operationsLesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operationsPVS-Studio
 
Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...
Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...
Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...IJERD Editor
 
Bit manipulation in atmel studio for AVR
Bit manipulation in atmel studio for AVRBit manipulation in atmel studio for AVR
Bit manipulation in atmel studio for AVRPham Hoang
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAhmad Bashar Eter
 
Low Level Prog. (from 201-c).ppt
Low Level Prog. (from 201-c).pptLow Level Prog. (from 201-c).ppt
Low Level Prog. (from 201-c).pptLearnWithJCM
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniquesPrabhjit Singh
 
Ashish garg research paper 660_CamReady
Ashish garg research paper 660_CamReadyAshish garg research paper 660_CamReady
Ashish garg research paper 660_CamReadyAshish Garg
 
Development of an Algorithm for 16-Bit WTM
Development of an Algorithm for 16-Bit WTMDevelopment of an Algorithm for 16-Bit WTM
Development of an Algorithm for 16-Bit WTMIOSR Journals
 
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...inventionjournals
 
Vectorized VByte Decoding
Vectorized VByte DecodingVectorized VByte Decoding
Vectorized VByte Decodingindeedeng
 
Implementation of character translation integer and floating point values
Implementation of character translation integer and floating point valuesImplementation of character translation integer and floating point values
Implementation of character translation integer and floating point valuesغزالة
 

Similar to Bit manipulation (20)

C bitwise operators
C bitwise operatorsC bitwise operators
C bitwise operators
 
Bitwise Operations(1).pdf
Bitwise Operations(1).pdfBitwise Operations(1).pdf
Bitwise Operations(1).pdf
 
Cse lecture-4.2-c bit wise operators and expression
Cse lecture-4.2-c bit wise operators and expressionCse lecture-4.2-c bit wise operators and expression
Cse lecture-4.2-c bit wise operators and expression
 
Lesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operationsLesson 11. Pattern 3. Shift operations
Lesson 11. Pattern 3. Shift operations
 
Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...
Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...
Comparison of Adders for optimized Exponent Addition circuit in IEEE754 Float...
 
Bit manipulation in atmel studio for AVR
Bit manipulation in atmel studio for AVRBit manipulation in atmel studio for AVR
Bit manipulation in atmel studio for AVR
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
 
Low Level Prog. (from 201-c).ppt
Low Level Prog. (from 201-c).pptLow Level Prog. (from 201-c).ppt
Low Level Prog. (from 201-c).ppt
 
Programing techniques
Programing techniquesPrograming techniques
Programing techniques
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
 
Session2
Session2Session2
Session2
 
Ashish garg research paper 660_CamReady
Ashish garg research paper 660_CamReadyAshish garg research paper 660_CamReady
Ashish garg research paper 660_CamReady
 
L010137986
L010137986L010137986
L010137986
 
Development of an Algorithm for 16-Bit WTM
Development of an Algorithm for 16-Bit WTMDevelopment of an Algorithm for 16-Bit WTM
Development of an Algorithm for 16-Bit WTM
 
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
Implementation and Simulation of Ieee 754 Single-Precision Floating Point Mul...
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Vectorized VByte Decoding
Vectorized VByte DecodingVectorized VByte Decoding
Vectorized VByte Decoding
 
Implementation of character translation integer and floating point values
Implementation of character translation integer and floating point valuesImplementation of character translation integer and floating point values
Implementation of character translation integer and floating point values
 
Sc
ScSc
Sc
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 

Recently uploaded

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 

Recently uploaded (20)

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 

Bit manipulation

  • 1. MOHAN LAL SUKHADIYA UNIVERSITY University Computer Centre Submitted To: Sanjiv Agrawal Sir Submitted By: Priyanka Yadav(MCA 3rd sem) Topic: BIT MANIPULATION IN JAVA
  • 3. • As is well known, numbers are stored and operated as binaries inside a computer. So, bit manipulation, as named, is to manipulate binary bits of a number directly. E.g. 6 & 11 = 0x110 & 0x1011 = 0x0001 = 2 • Java provides extensive bit-manipulation capabilities for programmers who need to get down to the “bits-and-bytes” level. • Computers represent all data internally as sequences of bits. Each bit can assume the value 0 or the value 1. Bit Manipulation??
  • 4. Bitwise Operators Bitwise operators perform operations on the bits of their operands. The operands can only be byte, short, int, long or char data types. For example, if an operand is the number 48, the bitwise operator will perform its operation on the binary representation of 48 (i.e., 110000).
  • 5. Bitwise AND(&): -set to 1 if both bits are 1. Bitwise OR(|): -set to 1 if atleast one bit is 1.
  • 6. Bitwise exclusive OR: -set to one if only 1 bit is set to 1. Bitwise ~: -All 1’s become 0 ‘s and all 0’s become 1’s.
  • 7. The left shift operator will shift the bits towards left for the given number of times. int a=2<<1; Let’s take the binary representation of 2 assuming int is 1 byte for simplicity. Position 7 6 5 4 3 2 1 0 Bits 0 0 0 0 0 0 1 0 Now shifting the bits towards left for 1 time, will give the following result Position 7 6 5 4 3 2 1 0 Bits 0 0 0 0 0 1 0 0 Left Shift <<:NOTE: Now the result in decimal is 4. If you left shift like 2<<2, then it will give the result as 8. Therefore left shifting 1 time, is equal to multiplying the value by 2.
  • 8. The right shift operator will shift the bits towards right for the given number of times. int a=8>>1; Let’s take the binary representation of 8 assuming int is 1 byte for simplicity. Position 7 6 5 4 3 2 1 0 Bits 0 0 0 0 1 0 0 0 Now shifting the bits towards right for 1 time, will give the following results Position 7 6 5 4 3 2 1 0 Bits 0 0 0 0 0 1 0 0 Right Shift >>: Now the result in decimal is 4. Right shifting 1 time, is equivalent to dividing the value by 2. NOTE:
  • 11. BITSET CONSTRUCTOR: Sno Constructor Description 1. BitSet( ) This constructor creates a new bit set. 2. BitSet(int size) This constructor allows you to specify its initial size, i.e., the number of bits that it can hold. All bits are initialized to zero
  • 12. Sno Method Description 1. void and(BitSet set) This method performs a logical AND of this target bit set with the argument bit set. 2. void andNot(BitSet set) This method clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet. 3. int cardinality( ) This method returns the number of bits set to true in this BitSet. 4. void clear( ) Zeros all bits. 5. void clear(int index) Zeros the bit specified by index. BITSET METHODS:
  • 13. 6. void set(int bitIndex) Sets the bit specified by index to true. 7. void set(int bitIndex, boolean v) This method sets the bit at the specified index to the specified value. 8. void set(int startIndex, int endIndex) Sets the bits from(inclusive) startIndex to endIndex (exclusive)to true. 9. String toString( ) Returns the string equivalent of the bit set. 10. void xor(BitSet bitSet) This method performs a logical XOR of this bit set with the bit set argument.
  • 14. 11. Object clone( ) This method clones the BitSet and produces a new BitSet that is equal to it. 12. int hashCode() This method returns a hash code value for this bit set. 13. void flip(int index) method sets the bit at the specified index to the complement of its current value. 14. int length() This method returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. 15. boolean get(int index) This method returns the value of the bit with the specified index. 16. boolean intersects(BitSet set) This method returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet.
  • 15. import java.util.BitSet; public class BitSetDemo { public static void main(String args[]) { BitSet bitset1=new BitSet(8); BitSet bitset2=new BitSet(8); // assign values to bitset1 bitset1.set(0); bitset1.set(1); bitset1.set(2); bitset1.set(3); bitset1.set(4);
  • 16. bitset1.set(5); // assign values to bitset2 bitset2.set(2); bitset2.set(4); bitset2.set(6); bitset2.set(8); bitset2.set(10); System.out.println("Bitset1:" + bitset1); System.out.println("Bitset2:" + bitset2); bitset1.andNot(bitset2); System.out.println("" + bitset1); System.out.println("" +bitset1.cardinality());
  • 17. System.out.println("" + bitset1.toString()); System.out.println("" + bitset2.toString()); System.out.println("Bitset1:" + bitset1) bitset2 = bitset1.clone(); System.out.println("" + bitset1.hashCode()); System.out.println("" + bitset1.length()); System.out.println("" + bitset1.intersects(bitset2)); } }
  • 18. OUTPUT: Bitset1: 0 1 2 3 4 5 Bitset2: 2 4 6 8 10 {0, 1, 3, 5}//and not 6//cardinality {0, 1, 2, 3, 4, 5}//string {2, 4, 6, 8, 10} 0 1 2 3 4 5 //clone 1261//hash code 6//length true //intersects
  • 20.
  • 21.
  • 22.
  • 23.