GUID Generation and Invalidation I'm updating an old ECS I wrote a couple years back, and I've written the following piece of code in order to generate a GUID:

const auto&amp; es::EntityStore::RequestEntity()
{
    if(m_freeEntities.size() != 0)
    {
        return ec::ENTITY_ID_INVALID;
    }
    //Get the lastmost EntityID
    auto eid = m_freeEntities.back(); //an std::vector&lt;uint64_t&gt;
    m_freeEntities.pop_back();
    //Get the index value of the EntityID
    uint32_t idx = 0;
    idx = eid &amp; 0XFFFFFFFF;
    //Get the version value of the EntityID
    uint32_t version = 0;
    version = ((eid &gt;&gt; 32) &amp; 0xFFFFFFFF) + 1;

    //Update the metadata at idx
    m_entityMetadata.at(idx).version = version;

    //Create &amp; return the new EntityID
    eid = version;
    eid = eid &lt;&lt; 32 | idx;
    //an unordered_set&lt;uint64_t&gt;
    return *m_livingEntities.insert(eid).first;
}


However, I believe it can be written out nicer, and perhaps with less machine code generated.

HOW THE GUID WORKS

I am using a uint64_t to represent my GUID. Reading left-to-right, the first 32 bits will represent the version of the GUID, while the last 32 bits represent the index of the GUID.

The index will be used to lookup items in other arrays and vectors, so it never changes.

The version, however, is used to differentiate between an object who might have the same index as another.

When a GUID is invalidated, it is stuck onto the end of a vector. 
When it is reused, it's version goes up by a counter of 1. 

So, if there are any game objects out there referencing ID 0 or belonging to ID 0, even though it occupies the same spot in the array because of it's index, it's GUID will be different because it's version is 1 higher than whatever GUID came before it.
What I see here:

1.

You use autos, whose bit length is unknown in compile time. Despite that, the code uses them as 64-bit values. Afaik you should have some structural guarantee that they are really 64-bit (f.e. instead of "auto" use simply uint64_t).

2.

This code:

uint32_t version = 0;
version = ((eid &gt;&gt; 32) &amp; 0xFFFFFFFF) + 1;


could be simplified to

uint32_t version = ((eid &gt;&gt; 32) &amp; 0xFFFFFFFF) + 1;