This question already has answers here:
Real world use cases of bitwise operators [closed]
(41 answers)
Closed 9 years ago.
So I'm currently in the process of learning C++ via the book 'SAMS teach yourself C++ in 1 hour a day'. So far it's been great - I've understood everything that's said and I have managed to use all of them in simple programs to practice them.
However I just got to the section on the Bitwise operators and I am completely stumped. I understand that you have &, ~, |, <<, >> etc, and I understand that each one performs a different action upon a number in its binary form, for ~ flips the numbers over.
The problem I have is that I just can't get my head around how and why you'd want to use them. It's all very well me being to take an int, flip the binary digits over and have another number, but how exactly does this help me in any way shape or form? I'd appreciate an explanation as to why you'd use each one, and if possible maybe an example?
Thanks everyone!
There are a lot of applications, but here are two examples. Suppose you have eight one-bit values stored in a one-byte container. Bitwise-and with a power of two will access individual bits easily.
If you're scanning for high intensity pixels in an RGB image, you can use bitwise-and with 128 against the three color values; that's a faster operation than another Boolean expression like R>128.
Related
This question already has answers here:
What is the fastest way to find if a number is even or odd?
(12 answers)
Closed 4 years ago.
Lets say I am checking for odd numbers:
(i % 2 == 1)
Will the compiler convert that operation to:
if(a & 1)
?
I am aware that bitwise operations are faster and that sometimes I will work with bits.
However my question is: If the normal arithmetic is more readable (in most instances), when shall I use bitwise if the compiler might convert it later?
Or shall I always use bitwise whenever is posible (even if it is less readable)?
You should always use the form that is better readable by human beings. If execution speed matters you have to profile your program and look at the assembly your compiler generates.
The only way to tell is to look at the assembly language code generated by the compiler. There are all kinds of optimizations that compilers do. The compile could easily change your modulus operator into a bit test instruction.
Performance is a product of system design; not coding.
The speed difference between your two examples is so small that it would be hardly noticed in nearly any application.
This question already has answers here:
Similarity String Comparison in Java
(12 answers)
Closed 4 years ago.
I am trying to get an algorithm that tells you what percentage of similarity there is between two sentences. I was thinking of creating a vector of chars. for each char there is in the sentence compare to all of the other chars in another sentence. then the amount of characters that are the same over the total amount of characters should give me that %... but If you guys have a faster, and more efficient way of doing this. then it would be greatly appreciated.
What you are looking for might be an algorithm such as the Vector Space Model [wiki link]. It is a common algorithm web search engines use to come up with relevant sites to strings, that users put in.
It is not the only algorithm that does this kind of thing (comparing text and giving a value for similarity), but most of them are not overly complicated and there are already libraries in C++, which implement them effectively for example Lucene or Xapian. If you skip through their docs you will almost certainly find a function that just takes two strings and gives back a scalar representation of their semantic similarity.
You could use the Levenshtein distance to work out the similarity between the two strings - see https://en.m.wikipedia.org/wiki/Levenshtein_distance for more info
This question already has an answer here:
What variable type for extremely big integer numbers?
(1 answer)
Closed 6 years ago.
"unsigned long int" keeps becoming 0. It has to be a really big integer that I can operate multiplication and modulo on.
Example: 1234^123 % 1234
If you're using gcc you can try __uint128_t or maybe use double instead unless you need to do bitwise operations
Two roads you got
1. Implement what you need by learning how to do large calculations using integer arrays with maths tricks for speed up OR
2. Use a library
I would suggest to go with 2nd option if you are not tied to use external library. One such bigNum library is ttmath
OR
If you have a option to switch to java, then java has builtin BigInteger, BigDecimal and more functionalities supported. Look at BigInteger javadoc
This question already has answers here:
Exponential Operator in C++
(4 answers)
Closed 9 years ago.
is there a reason? i know there's POW(), but that's a function. why doesn't it have ^ for exponents, when it seems like a very simple thing too add, that would be very convenient
C++'s operators are modeled after C's operators which in turn are modeled after general machine code instructions. The later have addition, subtraction, shift, and , or , xor etc. They can have multiplication and perhaps even division. All handle integers are handled and sometimes even floating-point numbers too. But it would extremely rare for exponentiation to have direct processor support. So it's never been thought of as (and so therefore wasnever made into) a builtin operator. Having said all that, there's left-shift << which exponentiates powers of 2.
In some languages ^ is the sign of logical operation. I think is XOR operation.
So that is why you should use POW() in C++.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What USEFUL bitwise operator code tricks should a developer know about?
Hi,
What are some neat tricks with using bit-wise operations. I know that unless you're programming in C you won't have much encounters with operating on bit level. Nonetheless, there are some neat tricks that you can apply in even higher level languages. Here are a few that I already know.
bit mask: Can hold a collection of boolean values
XOR Swap: Swap 2 values in place without a third variable
XOR Linked List: Create a doubly linked list with each node only hold one address value
What are some others?
find whether a number is odd or not
(number & 1)