I am having a Dictionary<int,ulong>, where I want to store StudentId and his/her registered courses (which is guaranteed to be 2).
Now, As you can see, instead of saving 2 courseids into a List of Integers, i want to store them as ulong as ulong occupy 64 bits and int occupy 32 bits.
So my question is, how can i combine these 2 integer ids and store them into a ulong variable. I've tried with some Bitwise operation and shifting but unable to figure it out.
"Packing" the data of two
ints into 64 bits can be accomplished withoutulong, for example like this:ValueTuple<int,int>takes exactly as much space asulong, but it lets you access individualints through its properties.If you must use
ulong, here is one approach that lets you pack and unpackints:Demo.