Bitwise OR Calculator

Calculate A | B online and see which bit positions become active when either input contains 1.

Number A
Dec46 Hex0x2E
Number B
Dec74 Hex0x4A

OR Result

OR (|)
Dec0
Hex0x00
Bin00000000

Bit-by-Bit Visualization

1 Active bit 0 Diff bit 0 Included zero 0 Leading zero

OR Truth Table

Click a row to load values into A and B, or inspect how each gate behaves.

Input AInput BOR
000
011
101
111
Click a truth table row to test those values.

How the Bitwise OR Calculator Works

A bitwise OR calculator compares two values bit by bit. The result writes 1 when either input bit is 1, making OR useful for setting flags and combining masks.

OR Rule

The output bit is 1 when A, B, or both inputs contain 1 in that position.

Set Flags

OR can turn selected bits on without changing unrelated active bits.

Combine Masks

Merge several mask values into a single integer for configuration or permissions.

Column View

Aligned binary rows make it easy to confirm which columns produce 1.

Bitwise OR Calculation Steps

Use these steps to calculate a bitwise OR result by hand.

Convert and Align

  • Write both numbers in binary.
  • Pad leading zeros where needed.
  • Compare one bit column at a time.

Apply the OR Rule

  • Write 1 if either bit is 1.
  • Write 0 only when both input bits are 0.
  • Keep the selected output width.

Read the Result

  • Group bits into nibbles for hex.
  • Convert to decimal if needed.
  • Use the value to set or combine flags.

Bitwise OR Examples

OR examples show how bit positions become active when either input contributes a 1.

InputOperationResultUse
101110 | 1001010 OR 1101110 Combined active bits
00001111 | 11110000 OR 11111111 Byte fill
0x2E | 0x4A OR 0x6E Hex flag merge
46 | 74 OR 110 Decimal integer result

Why Use This Bitwise OR Calculator?

Use this OR calculator to set bits, combine masks, and verify packed options.

Flag Builder

Combine several option bits and see the single integer you would pass to an API or store in a database.

Hex and Decimal Output

Read the merged value as hex for code, decimal for storage, and binary for checking.

Fixed Widths

Match 8, 16, 32, or 64-bit types so the result is exactly what your program produces.

Union View

The bit grid highlights every column where either input contributes a 1.

Practical Uses for Bitwise OR

OR is widely used when software needs to enable one or more bit flags.

File Permissions

Read (4) | write (2) | execute (1) = 7, the building block of chmod modes.

Option Flags

APIs such as open() take O_WRONLY | O_CREAT | O_TRUNC as one argument.

Colour Packing

(r << 16) | (g << 8) | b builds a 24-bit colour from three channels.

Enable a Feature

settings |= FEATURE turns a feature on and is safe to repeat.

What the Bitwise OR Operator Does

Bitwise OR (|) compares two integers bit by bit and writes 1 whenever at least one of the two input bits is 1. The output bit is 0 only when both input bits are 0.

ABA | B
000
011
101
111

Where AND acts as a filter, OR acts as a merge. Every bit that is on in either operand ends up on in the result. OR never turns a bit off, so A | B is always greater than or equal to both A and B for non-negative values.

Worked Example: 46 | 74 Step by Step

Using the calculator’s default inputs:

  00101110   (46, 0x2E)
| 01001010   (74, 0x4A)
  --------
  01101110   (110, 0x6E)

Every column with a 1 in either row produces 1. The result 01101110 is 64 + 32 + 8 + 4 + 2 = 110. In hex, compare one nibble at a time: 2 | 4 = 6 and E | A = E, giving 0x6E.

There is a useful identity that connects OR, AND, and addition: A + B = (A | B) + (A & B). With the example values, 46 + 74 = 120, and 110 + 10 = 120. The shared bits are counted once by OR and once more by AND.

Setting Flags With OR

The most common use of OR is turning bits on without changing any others. Define each option as its own bit, then OR them together.

#define OPT_BOLD       (1u << 0)  /* 0001 */
#define OPT_ITALIC     (1u << 1)  /* 0010 */
#define OPT_UNDERLINE  (1u << 2)  /* 0100 */

unsigned style = OPT_BOLD | OPT_UNDERLINE;   /* 0101 = 5 */
style |= OPT_ITALIC;                          /* 0111 = 7 */

The compound assignment style |= OPT_ITALIC sets bit 1 and leaves bits 0 and 2 exactly as they were. Running it twice changes nothing, because OR is idempotent: x | x = x. That makes it safe to “enable” a flag that might already be enabled.

Unix File Permissions: OR in Practice

Unix permission bits are a classic OR example. Read, write, and execute are the values 4, 2, and 1. You combine them with OR:

PermissionBinaryOctal
read1004
write0102
execute0011
read | write1106
read | write | execute1117

So chmod 755 means owner 7 = 4|2|1, group 5 = 4|1, and others 5 = 4|1. System calls take the same approach with named constants: open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644) ORs three option bits into one integer argument.

Packing Values: Building Colours and Fields

OR is the second half of packing several small values into one integer. First shift each value into place, then OR the pieces together.

// Pack 8-bit red, green, blue channels into one 24-bit colour
const r = 0x1E, g = 0x90, b = 0xFF;
const rgb = (r << 16) | (g << 8) | b;   // 0x1E90FF (dodger blue)
console.log(rgb.toString(16));          // "1e90ff"

Because each channel occupies its own byte, none of the bits overlap and OR behaves exactly like addition. If fields do overlap, OR silently merges them, so mask each value to its width first, for example (g & 0xFF) << 8.

Bitwise OR in Python and JavaScript

flags = 0b0001 | 0b0100      # 5
flags |= 0b0010              # 7
print(f"{flags:04b}")        # 0111

# Python also uses | for sets and dict merging, which is unrelated to bits
{1, 2} | {2, 3}              # {1, 2, 3}
console.log(46 | 74);         // 110
console.log(3.7 | 0);         // 3  (truncates toward zero, a common idiom)
console.log(2 ** 31 | 0);     // -2147483648 (32-bit signed overflow)

The x | 0 idiom works because JavaScript converts operands to 32-bit integers before applying OR. It is only safe for values between −2,147,483,648 and 2,147,483,647. Use Math.trunc() for larger numbers.

Common Mistakes With Bitwise OR

  • Using || instead of |. Logical OR returns the first truthy operand, so 4 || 2 is 4, while 4 | 2 is 6.
  • Trying to clear a bit with OR. OR can only set bits. To clear one, use AND with an inverted mask: x & ~MASK.
  • Overlapping fields. When packing, forgetting to mask each part means a too-large value spills into its neighbour’s bits.
  • Precedence surprises. In C, a | b == c is parsed as a | (b == c). Add parentheses around bitwise expressions.

Frequently Asked Questions

Find answers to common questions about bitwise operations and this online tool.

What does bitwise OR do?

Bitwise OR compares two integers bit by bit and returns 1 wherever at least one input has a 1. It returns 0 only when both bits are 0. For example, 46 | 74 = 110.

What is the difference between | and || ?

| is bitwise OR and combines bits into a new integer. || is logical OR and returns a boolean (or, in JavaScript, the first truthy operand). 4 | 2 is 6, while 4 || 2 is true or 4.

How do I set a bit with OR?

Use value |= (1 << n). The bit at position n becomes 1 and all other bits keep their current values.

Can OR clear or turn off a bit?

No. OR can only turn bits on. To clear a bit, use AND with an inverted mask: value & ~(1 << n).

When is OR the same as addition?

When the two values have no 1 bits in common, A | B equals A + B. That is why packing non-overlapping fields with OR works. In general, A + B = (A | B) + (A & B).

Why does x | 0 remove decimals in JavaScript?

JavaScript converts both operands of | to 32-bit signed integers, which truncates the fractional part. It only works for values within the 32-bit range, so Math.trunc is safer for large numbers.