Set Bit (Bit Set)
Set a specific bit in a binary number to 1
Bit Set Calculator
Bit Operation: Set
This function sets (sets to 1) a specific bit in a binary number. The numbering starts with the rightmost bit at position 0.
Bit-Set Visualization
Example: 80 (hex) → Set Bit 2
Bit Set Operation
Target Bit = 1
Other bits remain unchanged
Logical OR operation with maskImportant Properties
- Bit position starts at 0 (right)
- Only the specified bit is set to 1
- Already set 1-bits remain unchanged
- Operation is idempotent (multiple executions = same effect)
Mathematical Foundations of Bit-Set Operation
The Bit-Set operation uses a logical OR operation with a special mask:
Mask Creation
n = bit position, << = left shift
Bit-Set Operation
Logical OR operation with the generated mask
Bit-Set Formulas and Examples
General Bit-Set Formula
Where x is the original number and n is the bit position
Step-by-Step Example: Set Bit 2 in 80 (hex)
1. Original: 10000000₂ (80 hex = 128 dec)
2. Bit Position: n = 2
3. Create Mask: 1 << 2 = 00000100₂
4. OR Operation: 10000000₂ | 00000100₂ = 10000100₂
5. Result: 84 hex = 132 dec
More Examples
Original: 1110₂ (14)
Mask: 0001₂
Result: 1110₂ | 0001₂ = 1111₂ (15)
Original: 11110000₂ (240)
Mask: 00001000₂
Result: 11111000₂ (248)
OR Operation (|) Truth Table
The OR operation sets a bit if it is 1 in the mask
Bit-Set Reference
Standard Example
Bit Values (Powers of 2)
Bit 0: 2⁰ = 1
Bit 1: 2¹ = 2
Bit 2: 2² = 4
Bit 3: 2³ = 8
Bit 4: 2⁴ = 16
Bit 5: 2⁵ = 32
Bit 6: 2⁶ = 64
Bit 7: 2⁷ = 128
Logical Operators
|: bitwise OR
&: bitwise AND
^: bitwise XOR (exclusive OR)
~: bitwise negation (NOT)
<<: left shift
Common Operations
Set Bit: x | (1 << n)
Clear Bit: x & ~(1 << n)
Toggle Bit: x ^ (1 << n)
Check Bit: (x >> n) & 1
Bit Manipulation - Detailed Description
Bit Set Operation
The bit-set operation is a fundamental bitwise operation that sets a specific bit in a binary number to 1, while leaving all other bits unchanged. This operation is essential for hardware control and systems programming.
• Target bit is always set to 1
• Other bits remain unchanged
• Operation is idempotent
• Based on logical OR operation
OR Mask Technique
The OR mask technique is the heart of the bit-set operation. A mask is created that contains a 1 at the desired position and 0s at all other positions. This mask is then OR-combined with the original number.
Mask Creation
1. Create bit pattern: 1 << n
2. OR operation: original | mask
3. Result: Bit n is set
Practical Applications
Bit-set operations are indispensable in hardware programming and system control. They enable targeted activation of features, flags, and control bits without affecting other system states.
• Activate hardware registers
• Set status flags
• Grant permissions
• Enable feature flags
OR Operation
The logical OR operation is the foundation of the bit-set operation. It returns 1 when at least one of the input bits is 1, thereby setting bits but never clearing them.
OR Properties
- 0 | 0 = 0 (both bits remain 0)
- 0 | 1 = 1 (bit is set)
- 1 | 0 = 1 (bit remains set)
- 1 | 1 = 1 (bit remains set)
Practical Bit-Set Examples
Hardware Activation
Scenario: Turn on LED
Register: 10100000₂
Set Bit 3: 10101000₂
Effect: LED at position 3 on
Status Flags
Scenario: Set ready flag
Status: 01110000₂
Set Bit 7: 11110000₂
Effect: System ready
Permissions
Scenario: Grant write permission
Rights: 101₂ (r-x)
Set Bit 1: 111₂ (rwx)
Effect: Full access granted
Programming Tips
- Use constants: #define BIT2 (1<<2)
- Define macros: #define SET_BIT(x,n) ((x) | (1<<(n)))
- Use bit fields: for structured data
- Debugging: Binary output for control
- Atomic operations: for thread safety
- Combine bitmasks: multiple bits simultaneously
|