The only thing that can help you in this situation is if you have devices less than or equal 2^16
. here you need to have hash table that maps each 24-bit
key to unique 16-bit
key . So while sending the packet send 16-bit
key and while retrieving check for your keys mapped value in the hash table. You can also hard code the 16-bit
key values for direct comparisons.
But if you have more than 2^16 devices then according to pigeon hole principle you will have 2 or more devices with same 16-bit
key then you cannot be sure which device the packet is meant for.
Pseudo Code for Sending & receiving packet:-
Send(BigKey,Data) {
// get 16-bit key from 24-bit one
SmallKey = HashMap.getValue(BigKey);
Packet = SmallKey + Data ;
}
Receive(Packet) {
(Key,Data) = split(Packet);
// get devices 16-bit key
SmallKey = HashMap.getValue(MyKey);
if(SmallKey==Key)
Process(Data)
else Error("Invalid Packet");
}
Note:- Hash map can be created using red-black trees,b-tree etc. which do lookup in O(logn). which is good enough for your application here.