Inserting a bit into an integer, means to insert a bit into a specific position of an integer, with the highest bit
removed. For example, for a one byte integer
11001010, if I insert a
1 at digit position 4, which is marked here
|, the most significant bit, which is the left most
1, would be eliminated and the rest of the 3 bits
on the left side of
| would be shifted to the left for 1 bit.
This is not hard to implement, but surprisingly I cannot find any existing code snippet for such a simple job.
Here is the code snippet I wrote for C, but it can be easily translated into any other language. Here,
T is any
1 2 3 4 5 6 7 8 9 10 11 12 13
Don’t hesitate to comment if you think there is a better (more elegant or faster) way.