Questions tagged [bitwise-operators]

Operators which are used to perform manipulation at bit-level.The programming languages are Byte oriented whereas the hardware is bit oriented.Therefore, being able to operate on bit level can be very important in programming,especially when the program directly interact with the hardware.

Bitwise operations perform boolean logic on pairs of individual bits within two or more numerical values. A bitwise "and" operation on the values 3 (binary 0011) and 5 (binary 0101) produces a result of 1 (0001), whereas bitwise "or" produces a result of 7 (0111).

3037 questions
3109
votes
27 answers

How to set, clear, and toggle a single bit

How can I set, clear, and toggle a bit?
JeffV
  • 52,985
  • 32
  • 103
  • 124
282
votes
6 answers

What is bit masking?

I am fairly new to C programming, and I encountered bit masking. What is the general concept and function of bit masking? Examples are much appreciated.
Mr.Z
  • 2,929
  • 3
  • 14
  • 6
281
votes
41 answers

Real world use cases of bitwise operators

What are some real world use cases of the following bitwise operators? AND XOR NOT OR Left/Right shift
Olivier Lalonde
  • 19,423
  • 28
  • 76
  • 91
239
votes
28 answers

Why do we usually use || over |? What is the difference?

I'm just wondering why we usually use logical OR || between two booleans not bitwise OR |, though they are both working well. I mean, look at the following: if(true | true) // pass if(true | false) // pass if(false | true) // pass if(false |…
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
232
votes
11 answers

Convert to binary and keep leading zeros

I'm trying to convert an integer to binary using the bin() function in Python. However, it always removes the leading zeros, which I actually need, such that the result is always 8-bit: Example: bin(1) -> 0b1 # What I would like: bin(1) ->…
Niels Sønderbæk
  • 3,487
  • 4
  • 30
  • 43
219
votes
15 answers

What is the difference between & and && in Java?

I always thought that && operator in Java is used for verifying whether both its boolean operands are true, and the & operator is used to do Bit-wise operations on two integer types. Recently I came to know that & operator can also be used verify…
Lavneesh
215
votes
19 answers

How does Python's bitwise complement operator (~ tilde) work?

Why is it that ~2 is equal to -3? How does ~ operator work?
bala
  • 2,231
  • 2
  • 14
  • 9
206
votes
10 answers

Go << and >> operators

Could someone please explain to me the usage of << and >> in Go? I guess it is similar to some other languages.
brianoh
  • 2,079
  • 2
  • 13
  • 3
194
votes
1 answer

What is the meaning of double tilde (~~) in Java?

When browsing the source code of Guava, I came across the following piece of code (part of the implementation of hashCode for the inner class CartesianSet): int adjust = size() - 1; for (int i = 0; i < axes.size(); i++) { adjust *= 31; …
bisgardo
  • 4,130
  • 4
  • 29
  • 38
161
votes
12 answers

Is it good practice to use the xor operator for boolean checks?

I personally like the exclusive or, ^, operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write if (boolean1 ^ boolean2) { //do it } than if((boolean1 && !boolean2) || (boolean2 &&…
Peter
  • 29,498
  • 21
  • 89
  • 122
150
votes
3 answers

What does |= (single pipe equal) and &=(single ampersand equal) mean

In below lines: //Folder.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly; Folder.Attributes |= FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System |…
SilverLight
  • 19,668
  • 65
  • 192
  • 300
143
votes
12 answers

Does any language have a unary boolean toggle operator?

So this is more of a theoretical question. C++ and languages (in)directly based on it (Java, C#, PHP) have shortcut operators for assigning the result of most binary operators to the first operand, such as a += 3; // for a = a + 3 a *= 3; // for…
CompuChip
  • 9,143
  • 4
  • 24
  • 48
140
votes
15 answers

How to create NS_OPTIONS-style bitmask enumerations in Swift?

In Apple's documentation about interacting with C APIs, they describe the way NS_ENUM-marked C-style enumerations are imported as Swift enumerations. This makes sense, and since enumerations in Swift are readily provided as the enum value type it's…
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
135
votes
9 answers

What are bitwise operators?

I'm someone who writes code just for fun and haven't really delved into it in either an academic or professional setting, so stuff like these bitwise operators really escapes me. I was reading an article about JavaScript, which apparently supports…
click
  • 1,385
  • 2
  • 10
  • 5
134
votes
4 answers

Effect of a Bitwise Operator on a Boolean in Java

The bitwise operators are supposed to travel variables and operate on them bit by bit. In the case of integers, longs, chars this makes sense. These variables can contain the full range of values enforced by their size. In the case of booleans,…
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
1
2 3
99 100