Questions tagged [bitwise-or]

A bitwise OR operation takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits among the operators. For questions specific to the operator |, use [or-operator] instead.

A bitwise OR operation takes two bit patterns of equal length and performs the logical OR operation on each pair of corresponding bits among the operators. The result in each position is 1 if any of the pair of bits in the corresponding positions are 1; Otherwise, the result is 0.

Read also the wikipedia article on Bitwise OR.

128 questions
59
votes
2 answers

How do I use Java's bitwise operators in Kotlin?

Java has binary-or | and binary-and & operators: int a = 5 | 10; int b = 5 & 10; They do not seem to work in Kotlin: val a = 5 | 10; val b = 5 & 10; How do I use Java's bitwise operators in Kotlin?
Water Magical
  • 769
  • 1
  • 7
  • 12
23
votes
2 answers

Why is ( Infinity | 0 ) === 0?

I'm fiddling around with bitwise operators in JavaScript and there is one thing I find remarkable. The bitwise or operator returns 1 as output bit if one of the two input bits are 1. So doing x | 0 always returns x, because | 0 has no effect: ( 1 |…
pimvdb
  • 141,012
  • 68
  • 291
  • 345
19
votes
6 answers

Why is "||" the symbol for or?

I know that || represents the logical operation "or", but I'm curious if anyone knows the history of choosing that symbol. Was it just because it happened to be an unused symbol on the keyboard?
Erty Seidohl
  • 3,858
  • 2
  • 28
  • 42
15
votes
2 answers

Does bitwise-or guarantee an evaluation ordering?

Say I have this code: unsigned int func1(); unsigned int func2(); unsigned int func3(); unsigned int x = func1() | func2() | func3(); Does C++ guarantee that func1() will be called first, then func2(), and then func3()? Or is the compiler allowed…
Jeremy Friesner
  • 57,675
  • 12
  • 103
  • 196
10
votes
4 answers

Bitwise operation exercise

I have the following exercise: The numbers n0 to n7 are bytes represented in binary system. The task is every bit to drop either to the bottom or if it meets another bit it stays above it. Here is a visual example: I realized that if I apply…
Todo
  • 657
  • 1
  • 9
  • 24
9
votes
3 answers

Bitwise (Bitshift) operations on 64-bit integers in C++

I'm trying to deal with bitboards, which requires me to set a particular bit in a 64-bit unsigned integer. To set bit i, I perform a bitwise OR with the bitboard in question, with a left shifted number. #include uint64_t kings = 0ULL; //…
Shreyas
  • 579
  • 2
  • 5
  • 17
7
votes
3 answers

How to reverse a bitwise OR operation?

Here's what I've done: 93 | 199 which returns 223 I understand that this is because 0b1011101 | 0b11000111 is 0b11011111 However, suppose I want to do the reverse operation. How do I get 0b1011101 from a bitwise operation between 0b11000111 and…
dopatraman
  • 11,801
  • 22
  • 74
  • 138
7
votes
2 answers

Why two bitwise or AVX instructions?

In AVX there are two instructions to do a bitwise-or VORPD and VORPS. The docs say: VORPD (VEX.256 encoded version) DEST[63:0] <- SRC1[63:0] BITWISE OR SRC2[63:0] DEST[127:64] <- SRC1[127:64] BITWISE OR SRC2[127:64] DEST[191:128] <- SRC1[191:128]…
Heygard Flisch
  • 131
  • 1
  • 3
6
votes
6 answers

How does C++ do bitwise "or" operations on negative numbers?

When I give to a variable such value: e = 17|-15; , I get -15 as an answer after compiling.I can't understand what arithmetic c++ uses. How does it perform a bit-wise OR operation on negative decimals?
Narek Margaryan
  • 324
  • 1
  • 4
  • 11
5
votes
5 answers

Bitwise OR on strings for large strings in c#

I have two strings(with 1's and 0's) of equal lengths(<=500) and would like to apply Logical OR on these strings. How should i approach on this. I'm working with c#. When i consider the obvious solution, reading each char and applying OR | on them,…
BetterLateThanNever
  • 836
  • 1
  • 9
  • 26
4
votes
2 answers

Why does declaring a combination of bit fields inside an enum produce a different result than declaring it outside of the enum?

Here, I have a list of subjects indicated by bit fields with an "Optional" field containing the optional subjects at the bottom. [Flags] enum Subjects { Art = 0b_0000_0001, Agriculture = 0b_0000_0010, English =…
Mars Bars
  • 55
  • 5
4
votes
1 answer

Why does the C/C++ bitwise XOR operator care about sign?

I've been struggling with some low-level messaging for quite some time and it turned out to be an issue with the checksum calculation. I thought the bitwise XOR operator didn't care about sign, so I was using a QByteArray to store the bytes, and…
José Tomás Tocino
  • 8,637
  • 5
  • 40
  • 71
4
votes
1 answer

TSLint not recognizing proper bitwise operators

Sadly, this valid code is considered negligent by the default setting of TSLint: export const NO_FLAG: number = 0x0000; export const DESTROY_FLAG: number = 0x0001; export const NULL_FLAG: number = 0x0100; export const START_FLAG: number =…
Kim Gentes
  • 977
  • 12
  • 29
4
votes
4 answers

Using bitwise or instead of std::max for buffer sizing

I was looking at some C++ networking code. When I went to find out how large the buffer was, I found something like this: static const uint32_t MAX_COMMAND_BUFFER = sizeof(struct S1) | sizeof(struct S2) | sizeof(struct S3) | …
T.E.D.
  • 41,324
  • 8
  • 64
  • 131
4
votes
2 answers

How does not | (Bitwise OR) work in SQL Server/SSIS 2012?

I've read about the Bitwise OR and it seems to be like it functions same as OR except it's faster. I read https://msdn.microsoft.com/en-us/library/ms186714(v=sql.110).aspx And here is the example they give: USE tempdb; GO SELECT a_int_value |…
1
2 3
8 9