Computing number of data blocks using bitwise operations
Given a chunk of data of size e.g 500 bytes, assume you want to make sure that the data size is a multiple of a single block size of e.g 32 bytes. For this you normally would divide the size of the total data by the size of the block size and see if the remaining is 0. This article shows you how to do the same thing in a different way; using only bitwise operations.
Example of determining number of blocks using bitwise operations
#include<stdio.h> int main() { int DataLen = 500; int BlockSize = 32; int BlockCount = 0; int UsableData = 0; UsableData = DataLen & (~(BlockSize-1)); BlockCount = UsableData/BlockSize; printf("Original data size is %d\n", DataLen); printf("Usable data is %d (%d blocks) \n", UsableData, BlockCount ); return 0; }

Comment