Heyloo ^_^

I've stumbled upon this code :D

#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_PATCH 2

#define VERSION ((VERSION_MAJOR << 16) | (VERSION_MINOR << 8) | VERSION_PATCH)

I've been searching for some time now, unable to understand what << 16 and << 8 does, and why.

I converted VERSION to string, handling it as an integer, and printed out the result being 65794, which doesn't contain ANY of the numbers defined as VERSION_***.

Please help me with this wondering :)

Ps. Sorry for lousy thread title, I was unable to come up with a proper title :i

Thanks ;)

If you printed it as hex ("%x"), you'd see 10102, which contains ALL the numbers defined as VERSION_***.

<< is a shift operator. A major is 1 shifted left by 16 bits, a minor is 1 shifted left by 8 bits, a patch not shifted at all. Now they can bi combined safely into the single value, because they occupy different bit positions there.

Comments
Useful, thanks :)

If you printed it as hex ("%x"), you'd see 10102, which contains ALL the numbers defined as VERSION_***.

<< is a shift operator. A major is 1 shifted left by 16 bits, a minor is 1 shifted left by 8 bits, a patch not shifted at all. Now they can bi combined safely into the single value, because they occupy different bit positions there.

Ahh great ;D I understand now :P

Except, if I may ask, how do I print them as hex, say we use the same VERSION #define?

Edited 6 Years Ago by Excizted: n/a

I don't know much about bit manipulation yet, but I know that in this context the tokens/operators << and | are "bitwise operators". operator<< is the "left shift" operator. If x and y are 16-bit integers and x==0000000000000001 (which is 16-bit binary for the decimal value 1) and y==0000000000000000 (decimal 0), when you enter y = x << 8 you are telling the program to shift all the bits in the value x to the left 8 positions and store the new value to y. After this operation, y becomes 0000000100000000 (16-bit binary for the decimal number 256). operator | is the "bitwise inclusive OR" operator. It combines the bits of multiple values into one value. Continuing from the previous example:
Define a new 16-bit integer, z=0000000000000000 (decimal 0). Now, z = (x | y) will produce the result z=0000000100000001 (decimal 257).

In your question, VERSION is a macro that you can put in your code in place of the long string of shifts and ORs to represent the version information.

I don't know much beyond that though. I can't help you with the display issue.

EDIT:
hmmm... nvm... nezachem and firstPerson beat me to it...

Edited 6 Years Ago by Fbody: n/a

Comments
Thank you for useful answer :)

If you printed it as hex ("%x"), you'd see 10102, which contains ALL the numbers defined as VERSION_***.

<< is a shift operator. A major is 1 shifted left by 16 bits, a minor is 1 shifted left by 8 bits, a patch not shifted at all. Now they can bi combined safely into the single value, because they occupy different bit positions there.

I don't know much about bit manipulation yet, but I know that in this context the tokens/operators << and | are "bitwise operators". operator<< is the "left shift" operator. If x and y are 16-bit integers and x==0000000000000001 (which is 16-bit binary for the decimal value 1) and y==0000000000000000 (decimal 0), when you enter y = x << 8 you are telling the program to shift all the bits in the value x to the left 8 positions and store the new value to y. After this operation, y becomes 0000000100000000 (16-bit binary for the decimal number 256). operator | is the "bitwise inclusive OR" operator. It combines the bits of multiple values into one value. Continuing from the previous example:
Define a new 16-bit integer, z=0000000000000000 (decimal 0). Now, z = (x | y) will produce the result z=0000000100000001 (decimal 257).

In your question, VERSION is a macro that you can put in your code in place of the long string of shifts and ORs to represent the version information.

I don't know much beyond that though. I can't help you with the display issue.

EDIT:
hmmm... nvm... nezachem and firstPerson beat me to it...

Thank you very much for answering though :)

int n = 15;
cout << hex;
cout << n << endl;

Well that's simple thanks, tho was more wondering how to convert it to a string? 8) <- puppy eyes :D

>> Well that's simple thanks, tho was more wondering how to convert it to a string?

You can use the stringstream.

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    const float PI = 3.14159265f;
    string  pie = "";
    stringstream convert;  //create stream object
    convert << PI;    //insert out value into our stream
    convert >> pie; //get the value in the stream as a string object

   return 0;
}

Or for practice, try to create a function that does that without the sstream.

Edited 6 Years Ago by firstPerson: n/a

int n = 15;
cout << hex;
cout << n << endl;

>> Well that's simple thanks, tho was more wondering how to convert it to a string?

You can use the stringstream.

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
    const float PI = 3.14159265f;
    string  pie = "";
    stringstream convert;  //create stream object
    convert << PI;    //insert out value into our stream
    convert >> pie; //get the value in the stream as a string object

   return 0;
}

Or for practice, try to create a function that does that without the sstream.

But that's what I already did and got a string containing 65794.

Well almost, but isn't it the same as using my conversion function:

#include <sstream>
#include <string>

std::istringstream stream;
stream << VERSION;

std::string result = stream.str();

Edited 6 Years Ago by Excizted: n/a

Oh, you mean you want the number in hex inside the string? I don't see a good reason for that. Whenever you need to print in hex, just convert the string into a int or a float and set the stream to print out in hex.

Well it's the other way around.
VERSION is hex, we agreed?
I wan't that number 10102 into an string, or integer :P

#include <iostream>
using std::cout;
using std::hex;
#include <string>
using std::string;
#include <sstream>
using std::ostringstream;

#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_PATCH 2

#define VERSION ((VERSION_MAJOR << 16) | (VERSION_MINOR << 8) | VERSION_PATCH)

int main()
{
   int version = VERSION;
   cout << "version = " << hex << version << "\n";

   ostringstream oss;
   oss << hex << VERSION;
   string text(oss.str().c_str());
   cout << "string = " << text << "\n";
   return 0;
}

/* my output
version = 10102
string = 10102
*/

Edited 6 Years Ago by Dave Sinkula: n/a

Comments
just what i needed
This question has already been answered. Start a new discussion instead.