what is the Difference between Bit masks and bit manipulation? - bit-manipulation

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.

Related

Choosing between 32 and 64 bit intrinsic CRC on Intel CPU

I need to calculate CRC in order to form a hash function on an INTEL machine and came up with the following two intrinsic functions:
_mm_crc32_u32
_mm_crc32_u64
In my project, I am dealing with 32-bit variables and my dilemma is between shifting and ORing each two variables (thus creating a 64-bit variable) and then using the 64-bit CRC or run the 32-bit CRC on each of the two 32-bit variables.
I can't find anywhere the amount of cycles that each one of these functions take, and from the Intel function specifications it is unclear which one is preferable.
The same dilemma also applies on the 16-bit version of the CRC function:
_mm_crc32_u16
I tried checking it by taking the time before and after the CRC. The results were pretty much the same. So I need a more sophisticated way of calculating it.
Don't use CRC for hash values. It's not the same kind of thing.
Use the murmurhash for classic computer science hashing needs (that is, not huge cryptographic strength hashes). That also has implementations for different widths.
I don't understand what you mean: you have two 32-bit values and want a hash of that? That might be sensible or might not, depending on why. Can you clarify what you are trying to accomplish?

Bit-wise operators and bit manipulation

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

What does performing a byteswap mean? [duplicate]

This question already has answers here:
Difference between Big Endian and little Endian Byte order
(5 answers)
Closed 8 years ago.
There is dozens of places discussing how to do different kinds of byteswapping, but I couldn't easily find a place explaining the concept and some typical examples of how the need to byteswap occurs.
So here is the question: what is byteswapping and why/when do I need to do it?
If examples are a good way to explain, I would be happy if they where in standard C++. Book references are appreciated, preferentially to Lippman's or Pratas C++ primers since those are the one I have available.
If I understand your question correctly, you're talking about big endian to little endian conversion and back.
That occurs because some microprocessors use little endian format to refer to memory, while others use big endian format.
The bytestreams on the internet are for example, big endian while your intel CPU works with little endian format.
Hence to translate from network to CPU or CPU to network, we need a conversion mechanism called byteswapping.
OSes provide ntohl() and htonl() functions for doing this.
As mentioned in the comments, byteswapping is the process of changing a values endianess from one to another. Lets say you have a value in your memory (left address is lowest):
DE AD BE EF <- big endian
This valu econsists of 4 bytes - in hexadecimal representation two digits are one byte.
If we now assume that the above value is encoded in big endian, then this would mean that the lowest order byte if the very first byte in memory - here the DE. The Intel x86 processor architecture works with little endian, that means the same value as above would look like this in memory:
FE BE AD DE <- little endian
These two values represent the same value, but have a different endianess.

C++: 32 vs 64 bit stream operations

If I write an int into a fstream in a 32 application and the read that int back in a 64 bit application, should I expect the value to be different? If so (and I presume it is), what is the best way to achieve architecture-independent stream operations?
If you read and write using operator<< and operator>>, it will be platform independent, assuming that the integer is small enough to fit in both types, since if will be written as text. If you use ostream::read and osteam::write, it will not be platform independent, since you will be writing binary data.
If you don't need raw performance, using a text format is the easiest way to achieve platform independence. If you need better performance, you should look at a serialization library. Boost has a good cross-platform one.
Well it depends if you write binary or in ASCII. If you write your numbers in ASCII (UTF-8) then the reading should produce the same result.
I would recommend that you use the boost::serializaton package to read and write data in a controlled and uniform manner.
However if it works in the opposite direction is not certain, i.e. from 64 bit to 32 bit. It depends on your compiler, if it compiles ints to 64 bits then you might write values that can not be read into 32 bit ints. Even if you write to a formatted stream.
However there are no guarantees as the size of an int in C++, just that is larger or equal in size to a short. It is up to the compiler.
If you want to be really sure you can use GMP to handle large integers, then validate the data automatically.

Bit manipulations/operations calculator?

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.