Bitwise operators applications

102 views
Skip to first unread message

Alexey Dvoretskiy

unread,
Jul 22, 2017, 11:36:14 PM7/22/17
to golang-nuts
Hello golang-nuts.

I'm new to Go language and have no solid experience with C/C++.
I was a database programmer with some Python and I was able to get around without bitwise operators easily.
Of course, I ran into tons of issues with performance, deployment and other stuff. That is why I switched to Go.

The question is what are practical applications of bitwise operations in Go and when I should use/learn them?

Thanks
Alex

Egon

unread,
Jul 23, 2017, 12:40:05 AM7/23/17
to golang-nuts
Generally when you ask that question you probably won't need to use them.

The main reasons you would use them:

1. bitflags -- allows to easily specify a set of 8 to 64 elements easily (https://play.golang.org/p/DZj9FerK19)
3. packing -- e.g. when you have seven 3 bit numbers then you can fit 21 of them into a 64bit number; to pack and extract you would need to use bit operations; similarly many packing algorithms use them for that reasons
4. bit parallel operations - when you have several packed numbers you can do some operations in bit parallel, e.g. you could add together 16 3bit numbers together in two computer cycles using a 64bit number.
5. There are also some algorithms that exploit bit operations, e.g. shift-or search http://www-igm.univ-mlv.fr/~lecroq/string/node6.html

+ Egon

Michael Jones

unread,
Jul 23, 2017, 1:05:43 AM7/23/17
to Egon, golang-nuts
...and...because that is how computers work so the understanding will itself be interesting and useful. It can be nice to see how things work "under the hood."

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Michael T. Jones
michae...@gmail.com

Stefan Nilsson

unread,
Jul 23, 2017, 7:40:23 AM7/23/17
to golang-nuts
There is a whole bunch of interesting and efficient (both in practice and in theory) searching and sorting algorithms that rely on bitwise operations. Only a few weeks ago, I wrote a short text about ints and bitwise operations:


It covers radix sorting, integer sorting in O(n log log n) time, Bloom filters, bit sets and few other things.

Jan Mercl

unread,
Jul 23, 2017, 7:49:53 AM7/23/17
to Alexey Dvoretskiy, golang-nuts
On Sun, Jul 23, 2017 at 5:36 AM Alexey Dvoretskiy <advore...@gmail.com> wrote:

> The question is what are practical applications of bitwise operations in Go and when I should use/learn them?

Evolution clearly figured out the advantage of repeating fylogenesis in ontogenesis. Programmers learning machine code first, assembler next, then probably C (Pascal, ...) do that to their later substantial advantage as well.

--

-j

Reply all
Reply to author
Forward
0 new messages