Forem

Cover image for Bitwise Operators in python
Aswin Barath
Aswin Barath

Posted on • Edited on

3 3 1 1 1

Bitwise Operators in python

When it comes to binary numbers, bitwise operators are the choice.

Bitwise operators are used to performing operations on binary numbers.

AND, OR, XOR operators

  • AND & operator sets each bit to 1 if both bits are 1.
  • OR | operator sets each bit to 1 if one of two bits is 1.
  • XOR ^ operator sets each bit to 1 if only one of two bits is 1. Alt Text Output:
AND 82
OR 2039
XOR 1957
Enter fullscreen mode Exit fullscreen mode

Ha Ha, surprised about the outputs?!
The outputs are a result of the binary numbers a and b which gets converted into an integer, each time bitwise operation is performed.

NOT operator

  • NOT ~ operator inverts all the bits.
  • In python, the number gets converted into an inverted signed number. Alt Text Output:
NOT -11
Enter fullscreen mode Exit fullscreen mode

Shift operators

  • left shift << operator shifts left by pushing zeros in from the right and let the leftmost bits fall off.
  • right shift >> operator shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off. Alt Text Output:
Right shift 277
Left shift 4444
Enter fullscreen mode Exit fullscreen mode

Best Resources

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging during the busy time of my freelancing work life. Here’s the link to all of my socials categorized by platforms under one place: https://linktr.ee/AswinBarath

Thank you so much for reading my blog🙂.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (2)

Collapse
 
melvin2016 profile image
MELVIN GEORGE

Thanks for sharing Aswin 😀

Collapse
 
aswinbarath profile image
Aswin Barath

You're welcome bro.
Stay tuned for more bite sized knowledge😇.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay