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.

Example: 80 (hex) = 128 (dec) = 10000000 (binary)
Bit Position (to set)
Position 0: rightmost bit | Position 1: second bit | Position 2: third bit, etc.
Results after Bit-Set Operation
Binary:
Octal:
Decimal:
Hexadecimal:

Bit-Set Visualization

Example: 80 (hex) → Set Bit 2
Original: 1 0 0 0 0 0 0 0
Position: 7 6 5 4 3 2 1 0
After Set Bit 2: 1 0 0 0 0 1 0 0
84 (hex) = 132 (dec)
Bit Set Operation

Target Bit = 1

Other bits remain unchanged

Logical OR operation with mask
Important 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
\[\text{Mask} = 1 \ll n\]

n = bit position, << = left shift

Bit-Set Operation
\[\text{Result} = \text{Original} \Or \text{Mask}\]

Logical OR operation with the generated mask

Bit-Set Formulas and Examples

General Bit-Set Formula
\[\text{set\_bit}(x, n) = x \Or (1 \ll n)\]

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
Set Bit 0 in 14 (dec):
Original: 1110₂ (14)
Mask: 0001₂
Result: 1110₂ | 0001₂ = 1111₂ (15)
Set Bit 3 in 240 (dec):
Original: 11110000₂ (240)
Mask: 00001000₂
Result: 11111000₂ (248)
OR Operation (|) Truth Table
A
B
A | B
Meaning

0
0
0
Both 0 → 0
0
1
1
One 1 → 1
1
0
1
One 1 → 1
1
1
1
Both 1 → 1

The OR operation sets a bit if it is 1 in the mask

Bit-Set Reference

Standard Example
Original: 80 (hex) Bit 2 set: 84 (hex) Difference: +4 (dec)
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.

Properties:
• 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.

Application Areas:
• 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

Is this page helpful?            
Thank you for your feedback!

Sorry about that

How can we improve it?


IT Functions

Decimal, Hex, Bin, Octal conversionShift bits left or rightSet a bitClear a bitBitwise ANDBitwise ORBitwise exclusive OR

Special functions

AiryDerivative AiryBessel-IBessel-IeBessel-JBessel-JeBessel-KBessel-KeBessel-YBessel-YeSpherical-Bessel-J Spherical-Bessel-YHankelBetaIncomplete BetaIncomplete Inverse BetaBinomial CoefficientBinomial Coefficient LogarithmErfErfcErfiErfciFibonacciFibonacci TabelleGammaInverse GammaLog GammaDigammaTrigammaLogitSigmoidDerivative SigmoidSoftsignDerivative SoftsignSoftmaxReLUSoftplusSwishStruveStruve tableModified StruveModified Struve tableRiemann Zeta

Hyperbolic functions

ACoshACothACschASechASinhATanhCoshCothCschSechSinhTanh

Trigonometrische Funktionen

ACosACotACscASecASinATanCosCotCscSecSinSincTanDegree to RadianRadian to Degree