Page 1 of 2

Most frequently used bitwise operators.

Posted: Mon Feb 04, 2013 12:59 pm
by sandras
From your experience, what are the most frequently used bitwise operators, when not dealing with hardware?

Re: Most frequently used bitwise operators.

Posted: Mon Feb 04, 2013 1:38 pm
by Kazinsal
AND and OR. Bitwise NOT is also up there. The only time I use XOR often is as a replacement for mov reg, 0.

Re: Most frequently used bitwise operators.

Posted: Mon Feb 04, 2013 3:36 pm
by Combuster
Probably AND at the top. The typical OR construct has competition from a substitute writing: optionflag1+optionflag2+optionflag3

I think shifts beat bitwise NOT. Especially since they make good divides/multiplies and the compiler knows that.

Re: Most frequently used bitwise operators.

Posted: Mon Feb 04, 2013 4:37 pm
by dozniak
BSR/BSL might be used for implementing bitmaps in e.g. memory manager in a very handy way on x86.

Re: Most frequently used bitwise operators.

Posted: Sun Mar 10, 2013 3:04 pm
by benjii
Uhm, probably AND, but I always use XOR when nulling something :-P.

Re: Most frequently used bitwise operators.

Posted: Sun Mar 10, 2013 8:39 pm
by Mikemk
signed shift when multiplying/dividing by powers of two

Re: Most frequently used bitwise operators.

Posted: Mon Mar 11, 2013 4:47 pm
by bewing
OR for adding bitflags to bitfields. AND for masking and testing flagbits. XOR for toggling bitflags on and off. Left shifts for indexing arrays of various sizes (and the occasional "multiply by 2"). I use them all lots. The ones I almost never use are NOT and rotations.

Re: Most frequently used bitwise operators.

Posted: Wed Mar 13, 2013 11:28 am
by Mikemk
NOT is useful for reversing byte sized boolean values that most operating systems and higher level languages foolishly waste your ram with.

Re: Most frequently used bitwise operators.

Posted: Mon Mar 18, 2013 11:44 am
by Love4Boobies
Depends on what the problem is. E.g.:
  • For cryptography, XOR
  • For bit flags and masks, AND/OR
  • For array indexing, shifting
  • For logic gates, NAND
  • For serialization, AND/shifting (for encoding, see Combuster's point on OR)
  • etc.
Why the survey?

Re: Most frequently used bitwise operators.

Posted: Mon Mar 18, 2013 3:29 pm
by Mikemk
Love4Boobies wrote:
  • For logic gates, NAND
Umm... aren't all bitwise operators a type of logic gate?

Re: Most frequently used bitwise operators.

Posted: Tue Mar 19, 2013 5:49 am
by iansjack
m12 wrote:
Love4Boobies wrote:
  • For logic gates, NAND
Umm... aren't all bitwise operators a type of logic gate?
No.

A logic gate normally refers to a circuit with one or more binary inputs and a single binary output. Clearly

13 & 5

(for example) does not fulfil these conditions. Logical operators, on the other hand, do; for example:

13 && 5

Re: Most frequently used bitwise operators.

Posted: Tue Mar 19, 2013 1:33 pm
by Mikemk
13 & 5 performs:

Code: Select all

  1101
& 0101
------
  0101
You are right. It's not a logic gate - it's [in this case] 4 gates.

Re: Most frequently used bitwise operators.

Posted: Wed Mar 20, 2013 11:16 am
by Love4Boobies
m12 wrote:Umm... aren't all bitwise operators a type of logic gate?
No. An operator is a mathematical function. For example, in the case of Boolean, there are 2^2^n operators that can be applied to an n-bit string (NAND is one of the 16 operators that can be applied on bit operand pairs and, because we are talking bitwise, we actually apply NAND to every corresponding bit pair individually). On the other hand, a logic gate is a physical device that implements a Boolean function. I hope you see the difference since it's a bit like confusing apples and arithmetic because you were taught the latter using the former.

Also, I think you missed my point. Most of the logic gates inside digital computers are actually NAND gates, which are cheaper and sometimes slower but on top of which all other constructs can be built.

Re: Most frequently used bitwise operators.

Posted: Wed Mar 20, 2013 11:33 am
by Owen
Love4Boobies wrote:Most of the logic gates inside digital computers are actually NAND gates, which are cheaper and sometimes slower but on top of which all other constructs can be built.
While the NAND or NOR gate can be used to construct all other logic gates... nobody actually does this

Re: Most frequently used bitwise operators.

Posted: Wed Mar 20, 2013 12:25 pm
by Love4Boobies
No? I don't know where I got that from; I should take one of Coursera's courses on these matters. In that case, I don't know what the most common operation is when working with logic gates or whether one can be singled out.