Skip to main content

All Questions

3 votes
1 answer
957 views

How do you set and check a single bit in a programming language that does not support bitwise operations?

I'm using a programming language (more a scripting language) that does not support any bitwise operators like AND, OR, XOR, NOT (and shift as well). Common arithmetic and logical operations like + - *...
tigrou's user avatar
  • 279
14 votes
3 answers
42k views

Speeds of << >> multiplication and division

You can use << to multiply and >> to divide numbers in python when I time them I find using the binary shift way of doing it is 10x faster than dividing or multiplying the regular way. ...
Crizly's user avatar
  • 327
0 votes
3 answers
747 views

Using Power of 2 numbers to represent types

Let's say that we have some values, represented by power of 2: TYPE_1 = 1 TYPE_2 = 2 TYPE_3 = 4 TYPE_4 = 8 ... I need to store some of these types in one value. Example: To represent TYPE_1 with ...
Rodrigo's user avatar
  • 1,077
73 votes
2 answers
9k views

Why do bitwise operators have lower priority than comparisons?

Could someone explain the rationale, why in a bunch of most popular languages (see note below) comparison operators (==, !=, <, >, <=, >=) have higher priority than bitwise operators (&, |, ^...
SF.'s user avatar
  • 5,206
8 votes
4 answers
29k views

Ternary operator (condition ? foo : bar) and the XOR (^) operator

I have read in a recent code review that both ternary operator (condition ? foo : bar) and the XOR operator ^ are rarely used in Java. Is it true? If yes, is this because they are less readable? or ...
Kuldeep Jain's user avatar
20 votes
15 answers
5k views

What are bit operators good for? [closed]

Programming languages often come with various bit operators (e.g. bitwise left- and right shift, bitwise AND, OR, XOR...). These don't get used though very much, or at least such has my experience ...
Anto's user avatar
  • 11.2k
22 votes
6 answers
32k views

What are the advantages of using bitwise operations? [closed]

Following reading the latest CodeProject newsletter, I came across this article on bitwise operations. It makes for interesting reading, and I can certainly see the benefit of checking if an integer ...
billy.bob's user avatar
  • 6,559