| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Browse and search Google Drive and Gmail attachments (plus Dropbox and Slack files) with a unified tool for working with your cloud files. Try Dokkio (from the makers of PBworks) for free. Now available on the web, Mac, Windows, and as a Chrome extension!

View
 

Digit Mask

Page history last edited by Kenneth Finnegan 12 years, 10 months ago

The original usage for this macro was for converting integers to a string, which is better served by sprintf, but it could be used for other applications. 

 

Code:

#include <math.h>

#define MASKDIGIT(digitnum, n)  (n/pow(10, digitnum) - (n/pow(10,digitnum+1)*10))

 

Compile with:

cc -lm digitmask.c

 

Usage:

This macro could be used if you're expecting to return multiple single digit numbers from a function.

 

If you change the +1 to a +3 and *10 to *1000, you can use this as part of a function to display numbers nicely, like "1,000,000" instead of "1000000"

 


Extensions:

The width of the mask (one digit in this case) could be made variable realatively easily.

 


Sources:

http://codingexperiments.com/archives/324

Comments (0)

You don't have permission to comment on this page.