Bitwise Operators in C

C Programming Examples - Bitwise Operators

Bitwise operators are operators that are used to perform operations on data at the bit level. The six main types of bitwise operators are bitwise AND, bitwise OR, bitwise exclusive OR, unary operator, left shift operator, and right shift operator. The bitwise operator cannot be used directly with primitive data types like float, double, etc. The bitwise operator is mainly used with integer data types because of its compatibility.

Bitwise Operators:

  • Bitwise AND operator: It is denoted by the single ampersand sign (&). The result of the bitwise AND operation is 1 if both bits are 1 otherwise the result is always 0.
  • Bitwise OR operator: It is represented by a single vertical symbol (|). If any of the operands has a bit value of 1, the result will be 1; otherwise, it will be 0.
  • Bitwise exclusive OR operator: It is represented by the (^) symbol. The outcome of the bitwise Exclusive-OR operation is 1 if only one of the expressions has a value of 1; otherwise, the result is always 0.
  • Bitwise Complement operator: Bitwise complement is a unary operation that converts 1 to 0 and 0 to 1. It is represented by the tilde (~) symbol.
  • Bitwise Left-shift operator: It is an operator that shifts the number of bits to the left. The notation for the left-shift operator is <<.
  • Bitwise Right-shift operator: It is an operator that shifts the number of bits to the right. The notation for the left-shift operator is >>.

The following section contains various C programs on bitwise operations, integer bits, swapping and replacing integers, mathematical applications, and string position using bitwise operations. Each sample program on the bitwise operations includes a program description, C code, and program output. All examples have been compiled and tested on Windows and Linux systems.

Here is the listing of C programming examples on Bitwise Operations:

  1. C Programs on Bitwise Operators
  2. C Programs on Integer Bits
  3. C Programs on Swapping and Replacing Integers
  4. C Programs on Mathematical Applications using Bitwise Operations
  5. C Programs on String Positions
  6. C Programs on Strings using Bitwise Operations

advertisement
advertisement

1. C Programs on Integer Bits

Program Description
Addition using Bitwise Operators in C C Program to Perform Addition using Bitwise Operators
Multiply Number by 4 using Bitwise Operators in C C Program to Multiply Number by 4 using Bitwise Operators
Power of 2 using Bitwise in C C Program to Find Whether a Given Number is Power of 2 using Bitwise Operators
Power of 2 without Bitwise in C C Program to Check if a Given Number is a Power of 2 without using Bitwise
C Program to Check whether nth Bit is Set or not C Program to Check if nth Bit in a 32-bit Integer is Set or not
Reverse Bits using Bitwise in C C Program to Reverse All Bits of an 32-bit Integer using Bitwise

2. C Programs on Integer Bits

Program Description
C Program to Check if All Bits of Integer is One C Program to Check if All Bits of Integer is One
C Program to Count Trailing Zeros in Integer C Program to Count Trailing Zeros in Integer
Find Next Higher Value with Same Number of Set Bits in C C Program to Find Next Higher Value with Same Number of Set Bits
Find Highest Bit Set of Integer in C C Program to Find the Highest Bit Set for any Given Integer
C Program to Count Set Bits in an Integer C Program to Count Set Bits in an Integer
Count Leading Zeros in Number using Bitwise Operations in C C Program to Count Leading Zeros in Number using Bitwise Operations

3. C Programs on Swapping and Replacing Integers

Program Description
Replace Bits from Position in C C Program to Replace Bits from Specified Position
Replace Bits in Integer x from Bit Position a to b from Another Integer y in C C Program to Replace Bits in Integer x from Bit Position a to b from Another Integer y
Swap ith & jth Bits for a 32-Bit Integer in C C Program to Swap Two Bits for a 32-Bit Integer
Swap Numbers using Bitwise XOR Operation in C C Program to Swap Two Numbers without using Temporary Variables or Arithmetic Operator
Swap Integers without Temporary Variables & Bitwise Operations in C C Program to Swap Two Numbers without using Temporary Variables and Bitwise Operations

4. C Programs on Mathematical Applications using Bitwise Operations

Program Description
Alternate Pattern Program in C C Program to Check whether the Given Integer has an Alternate Pattern
Integer round up to next Lower Power of 2 in C C Program to Round Floor of Integer to Next Lower Power of 2
Round to Next Lower Multiple of 2 using Bitwise in C C Program to Round an Integer to Next Lower Multiple of 2 using Bitwise
Find MSB Position using Function in C C Program to Find MSB Position using Function

5. C Programs on String Positions

Program Description
C Program to Read Byte & Return All Bits C Program to Read Byte and Return All Bits
Find Position of 1-bits in C C Program to Find the Position of 1-bits
C Program to Check if Bit Position is set to One or not C Program to Check if Bit Position is Set to One or not

6. C Programs on Strings using Bitwise Operations

Program Description
Swap Two Numbers using Bitwise Operators in C C Program to Swap Two Numbers Using Bitwise Operators
Palindrome using Bitwise Operator in C C Program to Check Whether a Number is Palindrome or not using Bitwise Operator
C Program to Count Set Bits using Bitwise Operations C Program to Count Set Bits using Bitwise Operations
Flip the Bits to Convert X to Y in C C Program to Count the Number of Bits to be Flipped to Convert X to Y

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.