What is the occupancy of a 16-slot ring buffer with these indices, under the one-empty-slot convention?
write = 3; read = 14;
Answer: A
The write index has wrapped past the end, so the occupancy is the write index minus the read index taken modulo sixteen: three minus fourteen is minus eleven, and adding sixteen gives five. Doing that arithmetic with a mask rather than a conditional is why the size is a power of two.
Please log in or register to report this question.