SlideShare a Scribd company logo
1 of 11
Download to read offline
BITWISE OPERATORS
• bitwise operators operate on individual bits of
integer (int and long) values.
• If an operand is shorter than an int, it is
promoted to int before doing the operations.
• Negative integers are store in two's
complement form. For example, -4 is 1111
1111 1111 1111 1111 1111 1111 1100.
4/10/2013
Md.Samsuzzaman,Lecturer,Dept of
CCE,PSTU
2
Bitwise Operator
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Shift Right
>>> Shift Right zero fill
<< Shift left
& = Bitwise AND Assignment
|= Bitwise OR Assignment
^= Bitwise XOR Assignment
>>= Shift Right Assignment
>>>= Shift Right zero fill Assignment
<<= Shift Left Assignment
4/10/2013
Md.Samsuzzaman,Lecturer,Dept of
CCE,PSTU
3
Bitwise Operator
Applied to integer type – long, int, short, byte and char.
A B A | B A & B A^ B ~A
0 0 0 0 0 1
0 1 1 0 1 1
1 0 1 0 1 0
1 1 1 1 0 0
OPERATOR MEANING EXPLANATION EXAMPLE RESULT
~
Bitwise
unary NOT
This sign is used for
inverts all the bits
~42 213
& Bitwise AND
Produce a 1 bit if both
operands are also 1
otherwise 0
2 & 7 2
| Bitwise OR
either of the bits in the
operands is a 1,
then the resultant bit is a
1 otherwise 0
2 | 7 7
^
Bitwise
exclusive OR
if exactly one operand is
1, then the result
is 1. Otherwise, the
result is zero
2 ^ 7 5
>> Shift right
The right shift
operator, >>, shifts all of
the bits in a value to the
right a specified number
of times.
7 >> 2 1
>>>
Shift right
zero fill
shift a zero into the high-
order bit no matter
what its initial value was
-1 >>> 30 3
<< Shift left
The left shift operator, <<,
shifts all of the bits in a
value to the left a
specified number
of times.
2 << 2 8
&=
Bitwise AND
assignment
This is a short sign of AND
operation on same
variable
a=2
a& = 2
a = 2
4/10/2013
Md.Samsuzzaman,Lecturer,Dept of
CCE,PSTU
6
The Left Shift
byte a=8, b=24;
int c;
c=a<<2; 00001000 << 2 = 00100000=32
Java’s automatic type conversion produces unexpected
result when shifting byte and short values.
Example:
byte a = 64, b;
int i;
i = a<<2;
b= (byte) (a<<2);
i 00000000 00000000 00000001 00000000 = 256
b 00000000 = 0
Each left shift double the value which is equivalent to
multiplying by 2.
4/10/2013
Md.Samsuzzaman,Lecturer,Dept of
CCE,PSTU
7
The Right Shift
byte a=8, b=24;
int c ;
c=a>>2; 00001000 >> 2= 00000010=2
Use sign extension.
Each time we shift a value to the right, it divides that value by
two and discards any remainder.
The Unsigned Right Shift
byte a=8, b=24;
int c;
c=a>>>1 00001000 >>> 1= 00000100=4
public class BitewiseDemo
{
public static void main(String[] args)
{
System.out.println("<-------Bitewise Logical Operators------->");
String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int a = 2; // 0 + 0 + 2 + 0 or 0010 in binary
int b = 7; // 0 + 4 + 2 + 1 or 0111 in binary
int c = a | b; //Bitwise AND operator
int d = a & b; //Bitwise OR operator
int e = a ^ b; //Bitwise XOR(exclusive OR) operator
int f = ~a & a ; //Bitwise unary NOT operator, a = 0010 so ~a = 1101 hence f = ~a & a = 1101 & 0010 = 0000
int g = (~a & b) | (a & ~b);
System.out.println("The binary value of a = " + binary[a]);
System.out.println("The binary value of b = " + binary[b]);
System.out.println("The Bitwise OR : a | b = " +c);
System.out.println("The Bitwise AND : a & b = " +d);
System.out.println("The Bitwise XOR(exclusive OR) : a ^ b = “ +e);
System.out.println("The Bitwise unary NOT : ~a & a = “ +f);
System.out.println("~a&b|a&~b = " +g);
System.out.println();
System.out.println("<-------Bitewise Shift Operators------->");
System.out.println("The original binary value of a = " +binary[a] + " and
Decimal value of a = "+a);
a = a << 2; //Bitwise Left shift operator
System.out.println("The Left shift : a = "+a);
b = b >> 2; //Bitwise Right shift operator
System.out.println("The Right shift : b = b >> 2 = “ +b);
int u = -1;
System.out.println("The original decimal value of u = " +u);
u = u >>> 30; //Bitwise Unsigned Right shift operator
System.out.println("The Unsigned Right shift : u = u >>> 30 means u =
11111111 11111111 11111111 11111111 >>> 30 hence u = "+binary[u] + "
and Decimal value of u = "+u);
System.out.println();
System.out.println("<-------Bitewise Assignment Operators------->");
int p = 5;
System.out.println("The original binary value of p = " +binary[p] + " and
Decimal value of p = "+p);
p >>= 2; //Bitewise shift Right Assignment Operator
System.out.println("The Bitewise Shift Right Assignment Operators : p
>>= 2 means p = p >> 2 hence p = 0101 >> 2 so p =
"+binary[p] + " and Decimal value of p = "+p);
/*Same as you can check Bitwise AND assignment,Bitwise OR
assignment,Bitwise exclusive OR assignment,
Shift right zero fill assignment,Shift left assignment */
}
}
<-------Bitewise Logical Operators------->
The binary value of a = 0010
The binary value of b = 0111
The Bitwise OR : a | b = 7
The Bitwise AND : a & b = 2
The Bitwise XOR(exclusive OR) : a ^ b = 5
The Bitwise unary NOT : ~a & a = 0
~a&b|a&~b = 5
<-------Bitewise Shift Operators------->
The original binary value of a = 0010 and Decimal value of a = 2
The Left shift : a = 8
The Right shift : b = b >> 2 = 1
The original decimal value of u = -1
The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111
11111111 >>> 30 hence u = 0011 and Decimal value of u = 3
<-------Bitewise Assignment Operators------->
The original binary value of p = 0101 and Decimal value of p = 5
The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> 2 hence p =
0101 >> 2 so p = 0001 and Decimal value of p = 1

More Related Content

What's hot

What's hot (20)

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Nested loops
Nested loopsNested loops
Nested loops
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
C bitwise operators
C bitwise operatorsC bitwise operators
C bitwise operators
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Expression trees
Expression treesExpression trees
Expression trees
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Half adder & full adder
Half adder & full adderHalf adder & full adder
Half adder & full adder
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Python set
Python setPython set
Python set
 
Python : Operators
Python : OperatorsPython : Operators
Python : Operators
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 

Viewers also liked

Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in CTushar B Kute
 
OpenStack in Action 4! Thierry Carrez - From Havana to Icehouse
OpenStack in Action 4! Thierry Carrez - From Havana to IcehouseOpenStack in Action 4! Thierry Carrez - From Havana to Icehouse
OpenStack in Action 4! Thierry Carrez - From Havana to IcehouseeNovance
 
Java ppt
Java pptJava ppt
Java ppt044249
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in javaAtul Sehdev
 
Getting Started With OpenStack Icehouse Release
Getting Started With OpenStack Icehouse ReleaseGetting Started With OpenStack Icehouse Release
Getting Started With OpenStack Icehouse ReleaseKenneth Hui
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and OperatorsMarwa Ali Eissa
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression Ashim Lamichhane
 
File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CMahendra Yadav
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in JavaAbhilash Nair
 
Parity Generator and Parity Checker
Parity Generator and Parity CheckerParity Generator and Parity Checker
Parity Generator and Parity CheckerJignesh Navdiya
 
Monitoring and control system
Monitoring and control systemMonitoring and control system
Monitoring and control systemSlideshare
 

Viewers also liked (20)

Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in C
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Ppt java
Ppt javaPpt java
Ppt java
 
OpenStack in Action 4! Thierry Carrez - From Havana to Icehouse
OpenStack in Action 4! Thierry Carrez - From Havana to IcehouseOpenStack in Action 4! Thierry Carrez - From Havana to Icehouse
OpenStack in Action 4! Thierry Carrez - From Havana to Icehouse
 
Java ppt
Java pptJava ppt
Java ppt
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
Getting Started With OpenStack Icehouse Release
Getting Started With OpenStack Icehouse ReleaseGetting Started With OpenStack Icehouse Release
Getting Started With OpenStack Icehouse Release
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
OpenStack Icehouse Overview
OpenStack Icehouse OverviewOpenStack Icehouse Overview
OpenStack Icehouse Overview
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in C
 
Java String
Java String Java String
Java String
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Truth table
Truth tableTruth table
Truth table
 
Parity Generator and Parity Checker
Parity Generator and Parity CheckerParity Generator and Parity Checker
Parity Generator and Parity Checker
 
Monitoring and control system
Monitoring and control systemMonitoring and control system
Monitoring and control system
 

Similar to 15 bitwise operators

Java unit1 b- Java Operators to Methods
Java  unit1 b- Java Operators to MethodsJava  unit1 b- Java Operators to Methods
Java unit1 b- Java Operators to MethodsSivaSankari36
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statementsCtOlaf
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Himanshu Kaushik
 
Bit shift operators
Bit shift operatorsBit shift operators
Bit shift operatorsalldesign
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till nowAmAn Singh
 
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
 
3.OPERATORS_MB.ppt .
3.OPERATORS_MB.ppt                      .3.OPERATORS_MB.ppt                      .
3.OPERATORS_MB.ppt .happycocoman
 
Chapter 2 Decision Making (Python Programming Lecture)
Chapter 2 Decision Making (Python Programming Lecture)Chapter 2 Decision Making (Python Programming Lecture)
Chapter 2 Decision Making (Python Programming Lecture)IoT Code Lab
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsIHTMINSTITUTE
 
1 Standard Data types.pptx
1 Standard Data types.pptx1 Standard Data types.pptx
1 Standard Data types.pptxssuser8e50d8
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operatoreShikshak
 

Similar to 15 bitwise operators (20)

Java 2
Java 2Java 2
Java 2
 
Java unit1 b- Java Operators to Methods
Java  unit1 b- Java Operators to MethodsJava  unit1 b- Java Operators to Methods
Java unit1 b- Java Operators to Methods
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
C operators
C operatorsC operators
C operators
 
Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 
Bit shift operators
Bit shift operatorsBit shift operators
Bit shift operators
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
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
 
3.OPERATORS_MB.ppt .
3.OPERATORS_MB.ppt                      .3.OPERATORS_MB.ppt                      .
3.OPERATORS_MB.ppt .
 
Computer programming 2 Lesson 7
Computer programming 2  Lesson 7Computer programming 2  Lesson 7
Computer programming 2 Lesson 7
 
CA UNIT II.pptx
CA UNIT II.pptxCA UNIT II.pptx
CA UNIT II.pptx
 
Chapter 2 Decision Making (Python Programming Lecture)
Chapter 2 Decision Making (Python Programming Lecture)Chapter 2 Decision Making (Python Programming Lecture)
Chapter 2 Decision Making (Python Programming Lecture)
 
Bitwise operators
Bitwise operatorsBitwise operators
Bitwise operators
 
Python PCEP Logic Bit Operations
Python PCEP Logic Bit OperationsPython PCEP Logic Bit Operations
Python PCEP Logic Bit Operations
 
Report on c
Report on cReport on c
Report on c
 
1 Standard Data types.pptx
1 Standard Data types.pptx1 Standard Data types.pptx
1 Standard Data types.pptx
 
Lecture 11 bitwise_operator
Lecture 11 bitwise_operatorLecture 11 bitwise_operator
Lecture 11 bitwise_operator
 
C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
 

More from Ravindra Rathore

More from Ravindra Rathore (12)

Lecture 5 phasor notations
Lecture 5 phasor notationsLecture 5 phasor notations
Lecture 5 phasor notations
 
Introduction of reflection
Introduction of reflection Introduction of reflection
Introduction of reflection
 
Line coding
Line coding Line coding
Line coding
 
28 networking
28  networking28  networking
28 networking
 
26 io -ii file handling
26  io -ii  file handling26  io -ii  file handling
26 io -ii file handling
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
22 multi threading iv
22 multi threading iv22 multi threading iv
22 multi threading iv
 
21 multi threading - iii
21 multi threading - iii21 multi threading - iii
21 multi threading - iii
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - i
 
14 interface
14  interface14  interface
14 interface
 
Flipflop
FlipflopFlipflop
Flipflop
 

Recently uploaded

New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

15 bitwise operators

  • 1. BITWISE OPERATORS • bitwise operators operate on individual bits of integer (int and long) values. • If an operand is shorter than an int, it is promoted to int before doing the operations. • Negative integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111 1111 1100.
  • 2. 4/10/2013 Md.Samsuzzaman,Lecturer,Dept of CCE,PSTU 2 Bitwise Operator ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise XOR >> Shift Right >>> Shift Right zero fill << Shift left & = Bitwise AND Assignment |= Bitwise OR Assignment ^= Bitwise XOR Assignment >>= Shift Right Assignment >>>= Shift Right zero fill Assignment <<= Shift Left Assignment
  • 3. 4/10/2013 Md.Samsuzzaman,Lecturer,Dept of CCE,PSTU 3 Bitwise Operator Applied to integer type – long, int, short, byte and char. A B A | B A & B A^ B ~A 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1 0 0
  • 4. OPERATOR MEANING EXPLANATION EXAMPLE RESULT ~ Bitwise unary NOT This sign is used for inverts all the bits ~42 213 & Bitwise AND Produce a 1 bit if both operands are also 1 otherwise 0 2 & 7 2 | Bitwise OR either of the bits in the operands is a 1, then the resultant bit is a 1 otherwise 0 2 | 7 7 ^ Bitwise exclusive OR if exactly one operand is 1, then the result is 1. Otherwise, the result is zero 2 ^ 7 5
  • 5. >> Shift right The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. 7 >> 2 1 >>> Shift right zero fill shift a zero into the high- order bit no matter what its initial value was -1 >>> 30 3 << Shift left The left shift operator, <<, shifts all of the bits in a value to the left a specified number of times. 2 << 2 8 &= Bitwise AND assignment This is a short sign of AND operation on same variable a=2 a& = 2 a = 2
  • 6. 4/10/2013 Md.Samsuzzaman,Lecturer,Dept of CCE,PSTU 6 The Left Shift byte a=8, b=24; int c; c=a<<2; 00001000 << 2 = 00100000=32 Java’s automatic type conversion produces unexpected result when shifting byte and short values. Example: byte a = 64, b; int i; i = a<<2; b= (byte) (a<<2); i 00000000 00000000 00000001 00000000 = 256 b 00000000 = 0 Each left shift double the value which is equivalent to multiplying by 2.
  • 7. 4/10/2013 Md.Samsuzzaman,Lecturer,Dept of CCE,PSTU 7 The Right Shift byte a=8, b=24; int c ; c=a>>2; 00001000 >> 2= 00000010=2 Use sign extension. Each time we shift a value to the right, it divides that value by two and discards any remainder. The Unsigned Right Shift byte a=8, b=24; int c; c=a>>>1 00001000 >>> 1= 00000100=4
  • 8. public class BitewiseDemo { public static void main(String[] args) { System.out.println("<-------Bitewise Logical Operators------->"); String binary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int a = 2; // 0 + 0 + 2 + 0 or 0010 in binary int b = 7; // 0 + 4 + 2 + 1 or 0111 in binary int c = a | b; //Bitwise AND operator int d = a & b; //Bitwise OR operator int e = a ^ b; //Bitwise XOR(exclusive OR) operator int f = ~a & a ; //Bitwise unary NOT operator, a = 0010 so ~a = 1101 hence f = ~a & a = 1101 & 0010 = 0000 int g = (~a & b) | (a & ~b); System.out.println("The binary value of a = " + binary[a]); System.out.println("The binary value of b = " + binary[b]); System.out.println("The Bitwise OR : a | b = " +c); System.out.println("The Bitwise AND : a & b = " +d); System.out.println("The Bitwise XOR(exclusive OR) : a ^ b = “ +e); System.out.println("The Bitwise unary NOT : ~a & a = “ +f); System.out.println("~a&b|a&~b = " +g); System.out.println();
  • 9. System.out.println("<-------Bitewise Shift Operators------->"); System.out.println("The original binary value of a = " +binary[a] + " and Decimal value of a = "+a); a = a << 2; //Bitwise Left shift operator System.out.println("The Left shift : a = "+a); b = b >> 2; //Bitwise Right shift operator System.out.println("The Right shift : b = b >> 2 = “ +b); int u = -1; System.out.println("The original decimal value of u = " +u); u = u >>> 30; //Bitwise Unsigned Right shift operator System.out.println("The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111 11111111 >>> 30 hence u = "+binary[u] + " and Decimal value of u = "+u); System.out.println();
  • 10. System.out.println("<-------Bitewise Assignment Operators------->"); int p = 5; System.out.println("The original binary value of p = " +binary[p] + " and Decimal value of p = "+p); p >>= 2; //Bitewise shift Right Assignment Operator System.out.println("The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> 2 hence p = 0101 >> 2 so p = "+binary[p] + " and Decimal value of p = "+p); /*Same as you can check Bitwise AND assignment,Bitwise OR assignment,Bitwise exclusive OR assignment, Shift right zero fill assignment,Shift left assignment */ } }
  • 11. <-------Bitewise Logical Operators-------> The binary value of a = 0010 The binary value of b = 0111 The Bitwise OR : a | b = 7 The Bitwise AND : a & b = 2 The Bitwise XOR(exclusive OR) : a ^ b = 5 The Bitwise unary NOT : ~a & a = 0 ~a&b|a&~b = 5 <-------Bitewise Shift Operators-------> The original binary value of a = 0010 and Decimal value of a = 2 The Left shift : a = 8 The Right shift : b = b >> 2 = 1 The original decimal value of u = -1 The Unsigned Right shift : u = u >>> 30 means u = 11111111 11111111 11111111 11111111 >>> 30 hence u = 0011 and Decimal value of u = 3 <-------Bitewise Assignment Operators-------> The original binary value of p = 0101 and Decimal value of p = 5 The Bitewise Shift Right Assignment Operators : p >>= 2 means p = p >> 2 hence p = 0101 >> 2 so p = 0001 and Decimal value of p = 1