# 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

### How to set, clear, and toggle a single bit

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

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

### 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

### 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 |…
• 115,165
• 71
• 313
• 417

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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