Most frequently used bitwise operators.

Programming, for all ages and all languages.
sandras
Member
Member
Posts: 146
Joined: Thu Nov 03, 2011 9:30 am

Most frequently used bitwise operators.

Post by sandras »

From your experience, what are the most frequently used bitwise operators, when not dealing with hardware?
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: Most frequently used bitwise operators.

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Most frequently used bitwise operators.

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Most frequently used bitwise operators.

Post by dozniak »

BSR/BSL might be used for implementing bitmaps in e.g. memory manager in a very handy way on x86.
Learn to read.
User avatar
benjii
Posts: 14
Joined: Sat Oct 20, 2012 3:27 pm

Re: Most frequently used bitwise operators.

Post by benjii »

Uhm, probably AND, but I always use XOR when nulling something :-P.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Most frequently used bitwise operators.

Post by Mikemk »

signed shift when multiplying/dividing by powers of two
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Most frequently used bitwise operators.

Post 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.
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Most frequently used bitwise operators.

Post by Mikemk »

NOT is useful for reversing byte sized boolean values that most operating systems and higher level languages foolishly waste your ram with.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Most frequently used bitwise operators.

Post 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?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Most frequently used bitwise operators.

Post by Mikemk »

Love4Boobies wrote:
  • For logic gates, NAND
Umm... aren't all bitwise operators a type of logic gate?
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Most frequently used bitwise operators.

Post 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
Mikemk
Member
Member
Posts: 409
Joined: Sat Oct 22, 2011 12:27 pm

Re: Most frequently used bitwise operators.

Post 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.
Programming is 80% Math, 20% Grammar, and 10% Creativity <--- Do not make fun of my joke!
If you're new, check this out.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Most frequently used bitwise operators.

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Most frequently used bitwise operators.

Post 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
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Most frequently used bitwise operators.

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply