Actually C++14 has binary literals, which is a series of 1s and 0s preceded by 0b and the code you gave also works before C++17 and gives the correct result.
> I don't know how to test this content on my own:
> How and where do I replicate these examples
For now, you would want to treat everything above main() as a black box, which somehow allows you to test binary bit-wise operations by writing print( number_one, binary_operator, number_two ) ;
Actually C++14 has binary literals, which is a series of 1s and 0s preceded by 0b and the code you gave also works before C++17 and gives the correct result.
my c++ version number is (using JLBorges code) = c++98; that may be an issue to invoke binary literals?
Yes by hand but checked with similar example. From what I understand, all bits in a column must be 1 to satisfy AND requirement and that leaves column 1 as 1 only -right? Let e know if I am doing something wrong please.
If this were bitwise OR it evaluates to 1 if either bit (or both) is 1. Consequently, 5 | 6 evaluates like this:
5 | 6
0 1 0 1 //5
0 1 1 0 //6
----------
0 1 1 1 // 7
What would be a real life example or are these thought exercises--?
| or the binary OR operation. & is the AND operation. So 1|3|7 is 1. 1&3&7 is 1.
What would be a real life example or are these thought exercises--?
The binary ops can be used for set operations. And because they are usually directly supported by the CPU as a single instruction (they are almost trivial to implement in hardware), they are blindingly fast. Another use is when you want to store a bunch of independent flags. These can each be represented as a single bit in an integer. A real world example might be the good, bad, fail and eof bits of an std::stream.
Hi JL. I found some code that uses 8 registers versus 8 vals in a register for greater speed. The code is below. I don't understand how the author came up with hex vals he did for the bitwise functions. Basically assembler?
I just need to know how to code with bitwise functions in basic loops with flags and logical coparisons reduced to bitwise ops. Thank you. An example would be awesome.
The 8 bool flags need 8 registers. This is a lot. The 8 bit flags need only one register. If You put all flags in one register, it will be very fast to test. Are these logical registers or bitwise?
How would I convert the following code to bitwise operation, assembly. I'll be using OpenCV and any time I can save would help.
Be sure to profile the code when it's done to find performance problems. It's far too easy to over-optimize code that doesn't actually cause a problem.
1 2 3 4 5 6
for (int y(0); y<10; ++y)
{
if (x<y) {do stuff1};
if (x>y) { do stuff2 };
if (x == y) {do stuff3}
}
could be replaced by
1 2 3 4
int y;
for (y=0; y<x && y<10; ++y) do stuff1;
if (x == y) do stuff2;
for (; y<10; ++y) do_stuff3;
But honestly I highly HIGHLY doubt that it will make a difference. The code that does the loops is probably less than a dozen instructions. It's unlikely to be a performance problem.
OK, thank you. Some programmers are using actual hex addresses in their code, and other addresses as well. From a thought exercise point of view, how is this done. My question is really basic (I think)
Where did he get his numbers from e.g. 0x01, 0x02 etc.