0

lets say I have some binary number of size 2^n for example: 11110101 how can I find position of any zero in this ? (i want to fill a hole in data for example)

So my program would return in this case 4 or 6

EDIT: I do not need most significant byte, I want whatever byte is set to 0 , doesn't matter if it is first of las!

T4G2
  • 9
  • 2
  • Numbers are not variable length. It's pretty unlikely that your computer has an integer with exactly n bits unless n is one of a small set of numbers (8, 16, 32 and 64 are common). That makes the concept of "most significant unset bit" somewhat hard to interpret. However, "least significant unset bit" is fine. (You might want to search for "rightmost unset bit" instead. There's no right or left inside a computer, but people seem to like to talk as though there were.) I agree that the answer selected as a duplicate doesn't address your question, but I'll bet you can find an answer that does. – rici Jul 29 '21 at 02:15
  • You start with an iterator with the maximum possible value for a single bit, that is: a number with a bit shifted as far left as possible. In a loop, you check with an `&` operation if the unknown number has that bit set. If it has, you decrease the iterator by right-shifting one bit. Repeat until you find a bit that was not set. The loop must stop if the iterator reaches 0, meaning no bits were not set. – Cheatah Jul 29 '21 at 04:57
  • I think it was not obvious to everyone you are implementing a bitmap. – Cheatah Jul 29 '21 at 04:57

0 Answers0