I've always been a little confused with this stuff... I found snippet of code and understand what all of it is doing until this line... Seems overly obfuscated...

int tmp = 0xff000000 | ((int)(val) << 16 | (int)(val ) << 8 | (int)(val));

If you made a toy program in which you printed val in hex and then the resulting tmp in hex, do you think it would shed some light on it?

[edit]Hm. Maybe not. It doesn't look like the greatest example to work with.

[edit=2]...Maybe depending on what type val is.

#include <iostream>

int main()
{
   unsigned char val = 0xA5;
   int tmp = 0xff000000 | ((int)(val) << 16 | (int)(val ) << 8 | (int)(val));
   std::cout << "val = " << std::hex << (unsigned)val << "\n";
   std::cout << "tmp = " << std::hex << tmp << "\n";
   return 0;
}

/* my output
val = a5
tmp = ffa5a5a5
*/
commented: Great! bit MAGIC. +14

If val = 0x11223344 then that expression will 'or' the following

FF000000
            3344
            223344
            11223344

So the top byte is always FF. The bottom byte is always the bottom byte of val and the 2 middle bytes are an 'or'ed set of some of the val bytes.

That's about all that can be said without knowing what the application is, but it seems to be creating a simple code from val which might be used as a hash key or some form of security key?

Dave Sinkula's idea is good, but I think it is easier to see what goes on by printing the intermediate results too, but in binary instead of hex:

#include <iostream>
#include <limits>
#include <bitset>

int main()
{
    using namespace std;

    //int tmp = 0xff000000 | ((int)(val) << 16 | (int)(val ) << 8 | (int)(val));

    // breaking the expression down into component parts
    unsigned char val = 0xA5;
    unsigned tmp = 0xff000000;
    unsigned shift1 = (unsigned)(val);
    unsigned shift2 = (unsigned)(val) << 8;
    unsigned shift3 = (unsigned)(val) << 16;
    unsigned paste1 = (shift3 | shift2 | shift1);
    unsigned paste2 = tmp | paste1;

    cout << bitset<numeric_limits<unsigned>::digits>(tmp) << '\n'
         << bitset<numeric_limits<unsigned>::digits>(shift1) << '\n'
         << bitset<numeric_limits<unsigned>::digits>(shift2) << '\n'
         << bitset<numeric_limits<unsigned>::digits>(shift3) << '\n'
         << bitset<numeric_limits<unsigned>::digits>(paste1) << '\n'
         << bitset<numeric_limits<unsigned>::digits>(paste2) << '\n';
}
commented: Danke. +28

thanks everyone... I can now see exactly what it is doing...

good suggestions for future reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.