Skip to content

Introduction to Bit Shifting

A bit shift is a Bitwise operation where the order of a series of bits is moved, either to the left or right, to efficiently perform a mathematical operation.

Gopi Gorantala
Gopi Gorantala
1 min read

Table of Contents

We learned the most common Bitwise operations so far. Now, we are about to learn shifting, which is a common operation we use when we divide/multiply a number by 2.

What is shifting?

A bit shift is a Bitwise operation where the order of a series of bits is moved, either to the left or right, to efficiently perform a mathematical operation.

A bit shift moves each digit in a number’s binary representation left or right. The bit-shifting operators do precisely what their name implies: they shift bits. Here is a brief introduction to the different shift operators.

Types

There are three main types of shifts:

  1. Left shift: << is the left shift operator and meets both logical and arithmetic shifts’ needs.
  2. Arithmetic/signed right shift: >> is the arithmetic (or signed) right shift operator.
  3. Logical/unsigned right shift: >>> is the logical (or unsigned) right shift operator.
Note: <<< is not an operator because it would be redundant.

These operators can be applied to integer values int, long, short, byte or char.

In some languages, applying the shift operators to any datatype smaller than int automatically resizes the operand to be an int.

Bit ManipulationData Structures and Algorithms

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things