数据结构和算法

DSA - 主页 DSA - 概述 DSA - 环境设置 DSA - 算法基础 DSA - 渐近分析

数据结构

DSA - 数据结构基础 DSA - 数据结构和类型 DSA - 数组数据结构

链接列表

DSA - 链接列表数据结构 DSA - 双向链接列表数据结构 DSA - 循环链表数据结构

堆栈 &队列

DSA - 堆栈数据结构 DSA - 表达式解析 DSA - 队列数据结构

搜索算法

DSA - 搜索算法 DSA - 线性搜索算法 DSA - 二分搜索算法 DSA - 插值搜索 DSA - 跳跃搜索算法 DSA - 指数搜索 DSA - 斐波那契搜索 DSA - 子列表搜索 DSA - 哈希表

排序算法

DSA - 排序算法 DSA - 冒泡排序算法 DSA - 插入排序算法 DSA - 选择排序算法 DSA - 归并排序算法 DSA - 希尔排序算法 DSA - 堆排序 DSA - 桶排序算法 DSA - 计数排序算法 DSA - 基数排序算法 DSA - 快速排序算法

图形数据结构

DSA - 图形数据结构 DSA - 深度优先遍历 DSA - 广度优先遍历 DSA - 生成树

树数据结构

DSA - 树数据结构 DSA - 树遍历 DSA - 二叉搜索树 DSA - AVL 树 DSA - 红黑树 DSA - B树 DSA - B+ 树 DSA - 伸展树 DSA - 尝试 DSA - 堆数据结构

递归

DSA - 递归算法 DSA - 使用递归的汉诺塔 DSA - 使用递归的斐波那契数列

分而治之

DSA - 分而治之 DSA - 最大最小问题 DSA - 施特拉森矩阵乘法 DSA - Karatsuba 算法

贪婪算法

DSA - 贪婪算法 DSA - 旅行商问题(贪婪方法) DSA - Prim 最小生成树 DSA - Kruskal 最小生成树 DSA - Dijkstra 最短路径算法 DSA - 地图着色算法 DSA - 分数背包问题 DSA - 作业排序截止日期 DSA - 最佳合并模式算法

动态规划

DSA - 动态规划 DSA - 矩阵链乘法 DSA - Floyd Warshall 算法 DSA - 0-1 背包问题 DSA - 最长公共子序列算法 DSA - 旅行商问题(动态方法)

近似算法

DSA - 近似算法 DSA - 顶点覆盖算法 DSA - 集合覆盖问题 DSA - 旅行商问题(近似方法)

随机算法

DSA - 随机算法 DSA - 随机快速排序算法 DSA - Karger 最小割算法 DSA - Fisher-Yates 洗牌算法

DSA 有用资源

DSA - 问答 DSA - 快速指南 DSA - 有用资源 DSA - 讨论


DSA - Bitwise Algorithms

Introduction to Bitwise Algorithms

Bitwise algorithms are those that control the operation of individual bits of data. These algorithms are mainly used for enhancing operational speed and memory efficiency, especially when working with large data sets.

What is Bit?

Computers cannot understand the human language, it requires data encoded with bit. Here, Bit is the smallest unit of information in a computer. It is represented by binary digits that have only two values namely 0 and 1. Its other representations are YES or NO, TRUE or FALSE and ON or OFF.

The Bit Manipulation (operators)

Bit manipulation is a technique that involves performing low-level operations on individual bits of data, such as encrypting, toggling, shifting, or masking them. The operations performed on bit are called as bitwise operations. This operation requires one or two operands which are first converted to binary and then the operator is applied to each pair of corresponding bits. The outcome of this operation is also a binary number.

Bit manipulation can be useful for various purposes, such as −

  • It is useful in implementing low-level algorithms or data structures that require direct access to the binary representation of data, such as encryption, compression, hashing, or cryptography.

  • It can optimize the performance or memory usage by reducing the number of instructions or bytes needed to perform a given task, such as arithmetic, logic, or bit counting.

  • Bit manipulation is also used for manipulating hardware registers or device drivers that use specific bits to control the behavior or status of a device.

To perform Bit manipulation, we use various bitwise operators. They are explained below −

Bitwise AND operator (&)

The single ampersand sign (&) denotes the bitwise AND operator. It accepts two operands and returns 1 if both bits are 1, otherwise returns 0.

AND Operation

Example

In the following example, we are going to illustrate the AND operation in various programming languages.

#include <stdio.h>
int main() {
   int valOne = 8;
   int valTwo = 9; 
   int output = valOne & valTwo;
   printf("Result of AND operation: %d
", output); 
   return 0;
}
#include <iostream>
using namespace std;
int main()
{
   int valOne = 8;
   int valTwo = 9; 
   int output = valOne & valTwo;
   cout << "Result of AND operation: " << output << endl; 
   return 0;
}
public class Main {
   public static void main(String[] args) {
      int valOne = 8;
      int valTwo = 9;
      int output = valOne & valTwo;
      System.out.println("Result of AND operation: " + output);
   }
}
valOne = 8
valTwo = 9
output = valOne & valTwo
print("Result of AND operation:", output)

Output

Result of AND operation: 8

Bitwise OR operator (|)

The single pipe sign (|) denotes the bitwise OR operator. It accepts two operands as a parameter value and returns 1 if either bit is 1, otherwise it returns 0.

OR Operation

Example

Following is the example that demonstrates the working of bitwise OR operator in different programming languages.

#include <stdio.h>
int main() {
   int valOne = 8;
   int valTwo = 9; 
   int output = valOne | valTwo;
   printf("Result of OR operation: %d
", output); 
   return 0;
}
#include <iostream>
using namespace std;
int main() {
   int valOne = 8;
   int valTwo = 9; 
   int output = valOne | valTwo;
   cout << "Result of OR operation: " << output << endl; 
   return 0;
}
public class Main {
   public static void main(String[] args) {
      int valOne = 8;
      int valTwo = 9;
      int output = valOne | valTwo;
      System.out.println("Result of OR operation: " + output);
   }
}
valOne = 8
valTwo = 9
output = valOne | valTwo
print("Result of OR operation:", output)

Output

Result of OR operation: 9

Bitwise XOR operator (^)

The bitwise XOR operator is denoted by the Circumflex symbol (^). It also accepts two operands and returns 1 if the bits are different, otherwise it returns 0.

XOR Operation

Example

The following example shows how bitwise XOR operator works.

#include <stdio.h>
int main() {
   int valOne = 8;
   int valTwo = 9; 
   int output = valOne ^ valTwo;
   printf("Result of XOR operation: %d
", output); 
   return 0;
}
#include <iostream>
using namespace std;
int main() {
   int valOne = 8;
   int valTwo = 9; 
   int output = valOne ^ valTwo;
   cout << "Result of XOR operation: " << output << endl; 
   return 0;
}
public class Main {
   public static void main(String[] args) {
      int valOne = 8;
      int valTwo = 9;
      int output = valOne ^ valTwo;
      System.out.println("Result of XOR operation: " + output);
   }
}
valOne = 8
valTwo = 9
output = valOne ^ valTwo
print("Result of XOR operation:", output)

Output

Result of XOR operation: 1

Bitwise NOT operator (~)

The bitwise NOT operator is denoted by a single tilde sign (~). It accepts either 1 or 0 as an operand and returns the complement of that operand, which means it flips every bit from 0 to 1 and 1 to 0.

NOT Operation

Example

Following is the example illustrating the working of NOT operator in various programming languages.

#include <stdio.h>
int main() {
   int value = 0;
   int output = ~value;
   printf("Result of NOT operation: %d
", output); 
   return 0;
}
#include <iostream>
using namespace std;
int main() {
   int value = 0;
   int output = ~value;
   cout << "Result of NOT operation: " << output << endl; 
   return 0;
}
public class Main {
   public static void main(String[] args) {
      int value = 0;
      int output = ~value;
      System.out.println("Result of NOT operation: " + output);
   }
}
value = 0
output = ~value
print("Result of NOT operation:", output)

Output

Result of NOT operation: -1

Left-Shift operator (<<)

The double left arrow symbol (<<) denotes the left shift operator. It is used to shift the specified bits of the operand to the left by a given number of positions. It fills the vacated bits with zeros.

Left Shift Operation

Example

In the following example, we are going to illustrate the left-shift operation in various programming languages.

#include <stdio.h>
int main() {
   int value = 11;
   // int to binary conversion
   char newVal[5];
   for(int i = 3; i >= 0; i--) {
      newVal[3-i] = ((value >> i) & 1) ? '1' : '0';
   }
   newVal[4] = '\0';
   int output = value << 2; 
   printf("Binary representation of value: %s
", newVal);
   printf("Result of Left-Shift operation: %d
", output); 
   return 0;
}
#include <iostream>
#include <bitset>
using namespace std;
int main() {
   int value = 11;
   // int to binary conversion
   string newVal = bitset<4>(value).to_string(); 
   int output = value << 2; 
   cout << "Binary representation of value: " << newVal << endl;
   cout << "Result of Left-Shift operation: " << output << endl; 
   return 0;
}
public class Main {
   public static void main(String[] args) {
      int value = 11;
      // int to binary conversion
      String newVal = Integer.toBinaryString(value);
      int output = value << 2;
      System.out.println("Binary representation of value: " + newVal);
      System.out.println("Result of Left-Shift operation: " + output);
   }
}
value = 11
# int to binary conversion
newVal = format(value, '04b')
output = value << 2
print("Binary representation of value:", newVal)
print("Result of Left-Shift operation:", output)

Output

Binary representation of value: 1011
Result of Left-Shift operation: 44

Right-Shift operator (>>)

The double right arrow symbol (>>) denotes the right shift operator. It is used to shift the specified bits of the operand to the right by a given number of positions. It fills the vacated bits with either zeros or the sign bit (depending on whether the operand is signed or unsigned).

Right Shift Operation

Example

The following example demonstrates the working of Right-Shift operation in various programming languages.

#include <stdio.h>
int main() {
   int value = 11;
   // int to binary conversion
   char newVal[5];
   for(int i = 3; i >= 0; i--) {
      newVal[3-i] = ((value >> i) & 1) ? '1' : '0';
   }
   newVal[4] = '\0';
   int output = value >> 2; 
   printf("Binary representation of value: %s
", newVal);
   printf("Result of Right-Shift operation: %d
", output); 
   return 0;
}
#include <iostream>
#include <bitset>
using namespace std;
int main() {
   int value = 11;
   // int to binary conversion
   string newVal = bitset<4>(value).to_string(); 
   int output = value >> 2; 
   cout << "Binary representation of value: " << newVal << endl;
   cout << "Result of Right-Shift operation: " << output << endl; 
   return 0;
}
public class Main {
   public static void main(String[] args) {
      int value = 11;
      // int to binary conversion
      String newVal = Integer.toBinaryString(value);
      int output = value >> 2;
      System.out.println("Binary representation of value: " + newVal);
      System.out.println("Result of Right-Shift operation: " + output);
   }
}
value = 11
# int to binary conversion
newVal = format(value, '04b')
output = value >> 2
print("Binary representation of value:", newVal)
print("Result of Right-Shift operation:", output)

Output

Binary representation of value: 1011
Result of Right-Shift operation: 2