bitwise where

Pages: 12
Hi, I've been studying bitwise ops lately but I don't know how to test this content on my own:

for e.g.

0 1 1 0 // 6
0 0 1 1 // 3
-------
0 1 0 1 // 5

How and where do I replicate these examples. I hardly think you hard code them into you IDE environment right?

Thanks in advance.

0110 XOR 0011
I think in c++17 there will be binaries type.

imagine if you have the decimals :

int a = 6;

int b = 3;

int c = a ^ b ;
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.
ok,

int main ()
{

int y = 10111011 & 10111100;
cout<<y;

}

Poor but I had to start somewhere.
> 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 ) ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <bitset>
#include <limits>
#include <type_traits>

template < typename T >
typename std::enable_if< std::is_integral<T>::value && !std::is_enum<T>::value, std::string >::type
to_bits( T number )
{
    using unsigned_type = typename std::make_unsigned<T>::type ;
    constexpr std::size_t NBITS = std::numeric_limits<unsigned_type>::digits ;
    return std::bitset<NBITS>( unsigned_type(number) ).to_string() ;
}

#define print( a, bin_op, b ) \
    ( std::cout << to_bits(a) << " " #bin_op "\n" << to_bits(b) << "\n-----------------\n" \
              << to_bits( decltype(a)( a bin_op b ) ) << "\n\n" )

int main()
{
    const short a = 255 ;
    const short b = 1024 * 16 + 3 ;
    print( a, &, b ) ;
    print( a, |, b ) ;
    print( a, ^, b ) ;
}

0000000011111111 &
0100000000000011
-----------------
0000000000000011

0000000011111111 |
0100000000000011
-----------------
0100000011111111

0000000011111111 ^
0100000000000011
-----------------
0100000011111100

http://coliru.stacked-crooked.com/a/91d199d5d74718de
The easiest way to deal with this is to print the numbers in hex and get used to converting binary to/from hex.
Ok so I have implemented this code and recommendations. But I still have a basic question"

0 0 0 1 // 1
0 0 1 1 // 3
0 1 1 1 // 7
--------
0 0 0 1 // 1

ok I get 1|3|7 evaluates to 1.

But what does 1 mean besides the decimal value of the evaluation result? How is 1 used?

thx

tried JLBorges code (thank you) but got a compiler complaint. Tried the below and got an error as well:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\main.o" "..\\src\\main.cpp"
..\src\main.cpp:6:1: error: expected unqualified-id before 'if'
if (var==0b101)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

using namespace std;

int x= 0b11111100;

if (var==0b101)
  //...
switch (binliteral)
{
case 0B100:
       //...
       break;
case 0B101:
       //...
       break;
//...
}

^
Last edited on
Did you do that calculation by hand? Bitwise Or should evaluate to 1 if any of the operands are true, so you should end up with 0 1 1 1. That said...

But what does 1 mean besides the decimal value of the evaluation result?

It doesn't mean anything else; that is all it is. 1 just happens to be what you get if you ask the language to interpret those bits as an integer.
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?

1
2
3
if( __cplusplus == 201103L ) std::cout << "C++11\n" ;
else if( __cplusplus == 199711L ) std::cout << "C++98\n" ;
else std::cout << "pre-standard C++\n" ;


c++98
Last edited on
0 0 0 1 // 1
0 0 1 1 // 3
0 1 1 1 // 7
--------
0 0 0 1 // 1

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--?

Thank you all in advance
> tried JLBorges code but got a compiler complaint.
> g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\main.o" "..\\src\\main.cpp"

Compile with g++ -std=c++11 -Wall -Wextra -pedantic-errors ...
ok I get 1|3|7 evaluates to 1.
| 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.
> | or the binary OR operation. ... So 1|3|7 is 1

| is the bit-wise OR operator.
|| is the logical OR operator.
Both are binary operators.

So, 1|3|7 yields 7

0000000000000001 (1) |
0000000000000011 (3)
-----------------
0000000000000011 (3)

0000000000000011 (3) |
0000000000000111 (7)
-----------------
0000000000000111 (7)

1|3|7 == 7

http://coliru.stacked-crooked.com/a/ae9aa906aaa5accb
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?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef enum EmotiveFlags {
    E_ELATED       = 0x01,
    E_JOYOUS       = 0x02,
    E_NONPLUSED    = 0x04,
    E_BEWILDERED   = 0x08,
    E_ANNOYED      = 0x10,
    E_PUZZLED      = 0x20,
    E_ENRAGED      = 0x40,
    E_AMUSED       = 0x80
} EEmotiveFlags;

if( TestAny(flags, E_JOYOUS | E_ELATED | E_AMUSED) ) {
   //...
}
else if( TestAll(flags, E_ANNOYED | E_ENRAGED) && !TestAll(flags, E_AMUSED) ) {
   //...
}
Last edited on
like @technologist said a bool only needs a bit , use an _int8 to store 8 bools
How would I convert the following code to bitwise operation, assembly. I'll be using OpenCV and any time I can save would help.

sample

for (int y(0); y<10; ++y)
{
if (x<y) {do stuff};
if (x>y) { do stuff };
if (x == y) {do stuff}
}

like @technologist said a bool only needs a bit , use an _int8 to store 8 bools


How is this done -- to use int8 to store 8 bools?
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef enum EmotiveFlags {
    E_ELATED       = 0x01,
    E_JOYOUS       = 0x02,
    E_NONPLUSED    = 0x04,
    E_BEWILDERED   = 0x08,
    E_ANNOYED      = 0x10,
    E_PUZZLED      = 0x20,
    E_ENRAGED      = 0x40,
    E_AMUSED       = 0x80
} EEmotiveFlags;

if( TestAny(flags, E_JOYOUS | E_ELATED | E_AMUSED) ) {
   //...
}
else if( TestAll(flags, E_ANNOYED | E_ENRAGED) && !TestAll(flags, E_AMUSED) ) {
   //...
}
Last edited on
Pages: 12