Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Bitwise in JavaScript

Js Bitwise in JavaScript

? JavaScript Bitwise Operators


What are Bitwise Operators?

  • They operate on 32-bit integers at the binary level.

  • Useful for low-level programming, performance optimization, flags, masks, etc.

  • Convert operands to 32-bit signed integers internally.


List of Bitwise Operators

OperatorNameDescriptionExample
&ANDBits set in both operands5 & 3 ? 1
``ORBits set in either operand
^XOR (exclusive OR)Bits set in one operand but not both5 ^ 3 ? 6
~NOT (one's complement)Inverts bits~5 ? -6
<<Left shiftShift bits to the left5 << 1 ? 10
>>Signed right shiftShift bits to the right (sign-preserving)5 >> 1 ? 2
>>>Unsigned right shiftShift bits to the right (zero-fill)5 >>> 1 ? 2

How Bitwise Operators Work

Example with 5 and 3:

  • 5 in binary: 00000000000000000000000000000101

  • 3 in binary: 00000000000000000000000000000011

OperationBinary ResultDecimal Result
5 & 3000000000000000000000000000000011
`53`00000000000000000000000000000111
5 ^ 3000000000000000000000000000001106
~511111111111111111111111111111010-6

Example Usage

const flags = 5; // 0101 binary// Check if 1st bit is setif (flags & 1) {  console.log("1st bit is set");}// Set 2nd bitconst newFlags = flags | 2; // sets second bitconsole.log(newFlags); // 7 (0111 binary)

Notes

  • Bitwise operators always work on 32-bit integers.

  • Results are signed 32-bit integers.

  • Use unsigned right shift (>>>) to avoid sign extension.


Want me to show you how bitwise can be used in flags, masks, or performance tricks?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql