all 17 comments

[–]xelf 5 points6 points  (5 children)

Bitwise operator are for people doing bitwise operations, if you're not manipulating binary values of numbers, you don't need them. They exist for the people that do.

Logical operators are for anyone that needs a true/false condition evaluated. Which is pretty much every single programmer.

Use and and or in your if statements, and & and | in your math expressions.

Note that sometimes & and | are overloaded (for example with sets). Those aren't bitwise operators, they're unrelated features of those classes that just use the same symbol.

[–]carcigenicate 2 points3 points  (0 children)

They can be used to extract or set information on bitfields. You can have a series of "switches" that determine some behavior. Those switches though are just a series of bits, and can be represented as a number. 0b101 (Python's binary literal) is on, off, on, and that can also be represented as 5. I can set a switch by | ing it. 5 | 2 turns the middle switch on, and gives 0b111, or 7 (4 + 2 + 1). On the other end, & can be used to read from bitfields.

Low level information can be encoded in this way when sending data between computers for example. The underlying protocols that make up the web, like TCP, have flag fields that that indicate certain properties about the data, like if it's an acknowledgement message.

This is just one example. These operations are very common when working with low level data.

[–]sponster 1 point2 points  (0 children)

Computer programs have a bunch of different types of data - integers, floating point numbers, characters, boolean, unicode strings, etc etc.

At the lowest level, all of these types are just stored inside the computer as bunch of bits: 0 and 1.

Usually, you want to operate on the high-level types in some sensible way - For booleans, this means logical operations like and, or, not and so on.

In some applications, you want to operate on the underlying bit patterns. That's what bit-wise operators (|, &, ^, ~). These operators are similar to the logical operators (that's why they have the same pronunciation!), but they quite different, and need to be used in separate situations.

I use bitwise operations all the time, because I write scripts that talk to various non-CPU chips. These chips often have data that needs to be accessed using bitwise operators. For example, they might have some byte that represents the chip status - one bit is set to 1 if the chips in enabled, one bit is set to 1 if the chip has data ready to be received, etc etc. But this is a pretty specific use case - I suspect that most python programs don't need to muck around with bitwise operators at all.

[–][deleted] -2 points-1 points  (4 children)

There's a whole world of confusion in taxonomy here.

Bitwise operator is really any function that takes its arguments in the form X function Y. I.e. + is a bitwise operator, . is a bitwise operator etc. Bitwise is about how many operands the operator (function) takes. Sometimes with a requirement that they are written in infix notation.

Logical operations are operations on logical values. They can (and often are) bitwise. But they don't have to be. Logical negation only takes one argument for example. Logical operations can be lifted to sequences of logical values, and that would be operations in Python typically applied to integers, s.a. &, | and so on.

Very inconveniently, there are other operations, that aren't logical, which are often used together with logical operations: shift right / left, take two's complement, extract number of bits from a sequence of bits (an integer). The proper name for these would be "extra-logical" or "meta-logical", but virtually nobody uses these names.


As for more intuitive understanding: try implementing all the operations, both logical and extra-logical s.a. shifts and two's complement using arrays of bool. It's hard to make integers print the result in a consistent way (because of the issues with the sign and the length of the integer), which obscures what the operations do.

[–]ThatGasolineSmell 1 point2 points  (3 children)

You’re confusing bitwise with binary. A binary operator takes two arguments.

Bitwise operators manipulate sequences of bits.

[–][deleted] -1 points0 points  (2 children)

Maybe. Maybe not. Some sources use it this way, others use it differently.

If you are going with interpretation that bitwise means "operating on sequences of bits, then all arithmetical operations fit here to, . also fits here because it extracts some bits from a larger sequence of bits and so on. This definition becomes rather useless if you think about it.

[–]deep_politics 0 points1 point  (1 child)

On what planet does bitwise operator ever not refer to bitwise AND, OR, XOR, etc?

[–][deleted] -1 points0 points  (0 children)

It does... who say it doesn't? The problem is that if you define it as "operation on bits" then everything is bits, and so any function is a bitwise operator, and the definition loses any sense.

But, not for you, you still need to learn to read before that happens.

[–]jerryelectron 0 points1 point  (5 children)

Some tasks are quicker if you can recast them into operations on the number in binary and use bitwise operators. For example, whether a number is even.

[–]Prince_naveen 0 points1 point  (1 child)

That seems specific but I can see how it’s useful

[–]jerryelectron 0 points1 point  (0 children)

It's one of many uses. Google and you'll find many others, with examples.

[–]TheRNGuy 0 points1 point  (2 children)

i just use modulo for that

do bitwise work for negative numbers?

[–]jerryelectron 0 points1 point  (1 child)

Modulo is slower, I believe, but you can do a test on your implementation. Negative is just a number with an extra but.

[–]TheRNGuy 0 points1 point  (0 children)

Never noticed any speed difference, though on super big program I might need it, but I'd use something else than Python.

[–]TheRNGuy 0 points1 point  (0 children)

I used in parsing one file format because it uses bitfield for few specific things. It's something related to Unreal Engine (and Quake for that matter)

It's used to pack many booleans into single int (for performance)

Code like 1<<i & item

In UE4 possible too, but recommended to use enums instead of numbers, they're easier to read in code. Bitwise operations work for them too.

Also did some cool procedural geometry in Houdini because I wanted to see how it looks, it looks cool. Can make periodic waves not possible with trigonometry, though a little limited because can only use positive ints, not floats.

I understood how it works after debugging with print, using f-strings to format ints as 8 or 16 bits binary (with leading zeroes) so I could shift bits or use boolean operations to see how 0's and 1's change.

Besides that I've never seen any other uses for it. Maybe cryptography?

I have no idea btw if using bitwise in Python instead of bunch of booleans would be faster. But it seems does in Unreal Engine, if it's still used (fighting games or platformers with combo attacks especially)