Link to home
Start Free TrialLog in
Avatar of TError104
TError104

asked on

unique 48 bits number to unique 32 bits number

Hi,
as stated in the header, does anyone know an algorithm or something like that which enables me to use a 48 bit unique number and store it as a unique 32 bit number??
Regards
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you know the whole set of 48-bit numbers where the uniqueness needs to keep guaranteed ? If yes, you might take the index of each number as a unique 32 bit equivalence (I assume the size of the set is less than 4 billions).

If no, you could simply count the 4bit numbers you have to convert and and return the count value as a unique id:

typedef unsigned short UI48[3];

unsigned int I48ToI32(UI48 ui48)
{
     static unsigned int count = 0;
     return ++count;
}

In case you could get the same input twice  you would need to store all numbers converted to a dictionary and check for existence before incrementing count.

struct UI48
{
    unsigned short us3[3];
    bool operator<(const UI48& u) const { return us3[0] < u.us3[0] ||
                                                ( us3[0] == u.us3[0] && us3[1] < u.us3[1]) ||
                                                ( us3[0] == u.us3[0] && us3[1] == u.us3[1]  && us3[2] < u.us3[2]);
                                                         }
    bool operator==(const UI48& u) const { return ( us3[0] == u.us3[0] && us3[1] == u.us3[1] && us3[2] == u.us3[2]); }
};

unsigned int I48ToI32(UI48 ui48)
{
     static unsigned int count = 0;
     static std::map<UI48, unsigned int> uimap;
     if (uimap.find(ui48) == uimap.end())
           uimap[ui48] = ++count;
     return  uimap[ui48];
}

Regards, Alex
First of all : what do you want to achieve ?

1) lossless compression : impossible as stated by sunnycoder (unless there are max. 2^32 valid values possible for the 48bit integer)

2) conversion of a 48bit value to a 32bit value : only possible if the maximum value of the 48bit integer is 2^32-1

3) something else : please specify