In C programming, setting a bit is the process of setting a specific bit of a binary number to 1. This operation is crucial in various applications, including memory management, data processing, and hardware control.
In this article, we will learn how to set a bit at a given position in a binary number. We will also explore how to set multiple bits simultaneously.
How to Set a Bit in C?
Setting a bit means setting its value to 1. In C, we can set a given bit using the OR operator (|) combined with a bit mask. Below is the truth table of OR:
From the above table, we can infer that:
- By ORing a bit with 1, the given bit is set to 1.
- By ORing a bit with 0, the given bit remains unchanged.
Setting a Specific Bit in C
To set a specific bit in a number, you can use a bit mask with the OR operator. In that bitmask number, only the bit you want to set is set to 1, and all other bits are set to 0.
Example:
Input: binary_number: 01100111
bit to set: 5th
Output: binary_number: 01100111
mask_used: 00100000
C
// C Program to Set a given bit of a binary number
#include <stdio.h>
int main()
{
// Binary: 01100111
unsigned int num = 103;
// Setting the 5th bit (0-based index)
unsigned int bit_position = 5;
// Create a mask with only the 5th bit set to 1
unsigned int mask = 1 << bit_position;
// Set the bit using OR
num = num | mask;
// Print the result
printf("Result: %u\n", num);
return 0;
}
Time Complexity: O(1)
Space Complexity: O(1)
Setting Multiple Bits in C
We can also set multiple bits by creating a mask with multiple bits set to 1. ORing the number with this mask will set all corresponding bits.
Example: Setting the 1st, 3rd, and 4th Bits
We can use the left shift operator to create a mask, but remember the 0-based indexing in the shifting.
C
// C Program to Set multiple bits of a binary number
#include <stdio.h>
int main()
{
// Binary: 01100111
unsigned int num = 103;
// Create a mask with the 1st, 3rd, and 4th bits set to
// 1
unsigned int mask = (1 << 0) | (1 << 2) | (1 << 3);
// Set the bits using OR
num = num | mask;
// Print the result
printf("Result: %u\n", num);
return 0;
}
Time Complexity: O(n), where n is the number of bits to be set.
Space Complexity: O(1)
Similar Reads
Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
C String Functions C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.Th
6 min read
Constants in C In C programming, constants are read-only values that cannot be modified during the execution of a program. These constants can be of various types, such as integer, floating-point, string, or character constants. They are initialized with the declaration and remain same till the end of the program.
3 min read
scanf in C In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.Example:C#include <stdio.h> int main() { int n; // Reading an integer input scanf("%d",
3 min read
Tokens in C In C programming, tokens are the smallest units in a program that have meaningful representations. Tokens are the building blocks of a C program, and they are recognized by the C compiler to form valid expressions and statements. Tokens can be classified into various categories, each with specific r
4 min read