I am new at using bit-wise operators and manipulating bits and I was wondering if someone knows some techniques or something that will help me to learn to do this properly.
Maybe some book or something that explains this well and how to apply it.
I highly recommend these resources:
Representation of Numbers
Binary Arithmetic
Low Level Bit Hacks You Absolutely Must Know
and absolute classic
Bit Twiddling Hacks
Related
any one can give me answer to this Question ! is Bit Masks and Bit Manipulation are same topics ? or different ?
Bitmasks are a type of bit manipulation, usually performed using the bitwise AND operator to read or clear a specific number of bits. It can also refer to setting, clearing, and toggling individual bits in a bit field. Good resources for learning about bit manipulation (as you requested): Bitwise operations, Bit Twiddling Hacks, Bit manipulation, and Masks.
For example, how can I use the result of 1000^1000 for arithmetic? I don't think there's a library that can accommodate that, all I see at most is 100 number digits.
Use an arbitrary-precision arithmetic library like GMP or Boost.Multiprecision.
What you are looking for is a library like GMP or Boost-Multiprecision or TTmath.
Or, you might challenge yourself to write a low level representation that handles longer than standard bit representations, and do arithmetic with it.
Stick with the first option though, if it does the job you have in mind.
I've made a class for working with bits for unsigned int(8, 16, 32...), and I've been trying to benchmark it compared to the bare bitwise operations but getting an accurate test of the speed of bitwise operations is complicated to say the least. The reason for the wrapper is that it is a lot less complicated to use.
Now, while this may be more academically useful than practically, I would like to know if it was possible to make a class wrapping around an int that is so transparent that it is like working with the int directly (with all possible operators) except with functions that let me manipulate it in certain ways, and if it is possible to be as fast (with a lot of inlining).
I'm okay with using any and all C++0x features to achieve this.
You can setup a custom class and overload all the relevant operators (arithmetic operators, comparison operators, bitwise operators, conversion operators) and supply your own behaviour as needed.
As far as I know, it would indeed be possible to make it behave like an int in almost all respects.
An inlining compiler will presumably do a very good job at removing most of the overhead, but to find out how good the results are, you will need to try it out.
Such a type would have little use for benchmarking, though - I'd rather repeat the very same operations until the relative error in your measurements get smaller. It's more or less impossible to time the speed of a something like a single bitwise operation as it really boils down to one or two machine instructions.
Templates?
Also, what is the difficulty in profiling your operations? The usual way is to do the same operation X times and then get the average. If you're on an Intel CPU you can read the CPU's timers before and after each operation and then get the average it has some flaws but should be relatively ok for your purposes
http://en.wikipedia.org/wiki/Time_Stamp_Counter
Also, you can look at the asm the compiler generates when your class is used and see if there are any areas where you can refactor your code so that it can do a better job at optimisations.
Yes, it's possible; but I think you might find it easier in the long run to just write some templated inline functions that you can call with various int8/int16/etc arguments. That way you don't have to worry about how to handle passing objects of your class to functions that expect regular integers, and vice versa. (Not that that isn't doable as well, but it might keep things simpler to avoid that issue entirely)
I am getting into bit manipulation in C/C++.
Is there a good calculator or program (executable or online), that makes it relatively convenient to study & test bit procedures?
I can do same work in Visual Studio or Eclipse, but relatively small program is easier and more convenient.
I use bincalc by Ding Zhaojie. It conveniently lets you see what's going on in hex and binary (and decimal if you care), and has a nice gadget for inputting binary or flipping bits.
http://sites.google.com/site/bincalc/
Free, but no source.
The calculator, calc, in Windows 7 has a 'Programmer' view (Alt + 3). It has most of the operations for bit manipulations.
bitwisecmd.com Supports conversion from decimal to binary and hehadecimal numbers as well as basic bitwise operations as AND, OR, XOR, NOT, left shift, right shift. Neatly arranges bits in binary numbers so you can see how this stuff works.
I use a good online tool that supports all the bitwise operations, so you don't need to install anything: http://www.convertforfree.com/bitwise-calculator/
This tool saved me a lot of time and effort.
Use this tool. It has a free online truth table generator as well as a calculator for bitwise operations.
Is there a nice e-book (preferably PDF, like Array and Pointers in C) explaining bitwise operations? Other resources also welcome, but I'm after a self-contained document, if possible.
Thanks
This isn't an ebook, but it's more of a "cookbook" of many applications of bitwise operations: Bit Twiddling Hacks.