Click here to Skip to main content
16,006,348 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralSimple Double Buffering Pin
Robert A29-Mar-03 9:37
Robert A29-Mar-03 9:37 
GeneralRe: Simple Double Buffering Pin
PJ Arends29-Mar-03 9:49
professionalPJ Arends29-Mar-03 9:49 
GeneralRe: Simple Double Buffering Pin
Brad Bruce29-Mar-03 9:59
Brad Bruce29-Mar-03 9:59 
Generalnumber of decimals in integer Pin
Jan Hirak29-Mar-03 7:54
Jan Hirak29-Mar-03 7:54 
GeneralRe: number of decimals in integer Pin
PJ Arends29-Mar-03 9:12
professionalPJ Arends29-Mar-03 9:12 
GeneralRe: number of decimals in integer Pin
Mike Nordell29-Mar-03 15:38
Mike Nordell29-Mar-03 15:38 
GeneralRe: number of decimals in integer Pin
Gary R. Wheeler30-Mar-03 6:02
Gary R. Wheeler30-Mar-03 6:02 
GeneralRe: number of decimals in integer Pin
Gary R. Wheeler30-Mar-03 5:59
Gary R. Wheeler30-Mar-03 5:59 
int CountDigits(int value)
{
    int digit_count = 0;

    do {
        digit_count++;
        value /= 10;
    } while (value != 0) {

    return digit_count;
}

or, even faster:
int CountDigitsFaster(int value)
{
    int digit_count = 1;
    if (value < 0) value = -value;
    if (value > 9) digit_count++;
    if (value > 99) digit_count++;
    if (value > 999) digit_count++;
    if (value > 9999) digit_count++;
    if (value > 99999) digit_count++;
    if (value > 999999) digit_count++;
    if (value > 9999999) digit_count++;
    if (value > 99999999) digit_count++;
    if (value > 999999999) digit_count++;
    return digit_count;
}

Note that CountDigitsFaster assumes that int's are 32-bit signed values. Both functions assume that the minus sign in negative values is not considered a 'digit'. If you're actually trying to compute the number of characters required to display the value, then you would have to take that into account as well. CountDigitsFaster looks like dumb code, but is significantly faster than any alternative I can think of. It requires 10 compares, 10 jumps, possibly a negation, and up to 9 increments. My original CountDigits, while it may exit with an early out for small values, requires an increment and a division per iteration, for up to 9 iterations.


Software Zen: delete this;
GeneralPerformance Counter Pin
Luis Ricardo29-Mar-03 7:48
Luis Ricardo29-Mar-03 7:48 
GeneralRe: Performance Counter Pin
Erik Juhl31-Mar-03 5:03
Erik Juhl31-Mar-03 5:03 
GeneralRe: Performance Counter Pin
Luis Ricardo31-Mar-03 11:54
Luis Ricardo31-Mar-03 11:54 
GeneralRe: Performance Counter Pin
jagadeesh_skm18-Mar-10 18:58
jagadeesh_skm18-Mar-10 18:58 
GeneralMFC Print Preview - help urgently Pin
Nehal Kanani29-Mar-03 7:47
Nehal Kanani29-Mar-03 7:47 
QuestionMDI, CFormView1 open CFormView2, How ? Pin
jensreichert29-Mar-03 7:12
jensreichert29-Mar-03 7:12 
AnswerRe: MDI, CFormView1 open CFormView2, How ? Pin
Brian Shifrin29-Mar-03 14:51
Brian Shifrin29-Mar-03 14:51 
GeneralMDI, CFormView1(with Doc-Class) open CFormView2 (without Doc-Class), How ? Pin
jensreichert29-Mar-03 20:04
jensreichert29-Mar-03 20:04 
QuestionWhich API's do i need to get the hWnd of the control under the cursor? Pin
Redeemer-dk29-Mar-03 6:57
Redeemer-dk29-Mar-03 6:57 
AnswerRe: Which API's do i need to get the hWnd of the control under the cursor? Pin
PJ Arends29-Mar-03 9:40
professionalPJ Arends29-Mar-03 9:40 
AnswerRe: Which API's do i need to get the hWnd of the control under the cursor? Pin
John R. Shaw29-Mar-03 10:36
John R. Shaw29-Mar-03 10:36 
GeneralCListCtrl and ImageList Pin
JamesLaing29-Mar-03 5:33
JamesLaing29-Mar-03 5:33 
GeneralRe: CListCtrl and ImageList Pin
Gary R. Wheeler29-Mar-03 5:49
Gary R. Wheeler29-Mar-03 5:49 
GeneralRe: CListCtrl and ImageList Pin
JamesLaing29-Mar-03 5:57
JamesLaing29-Mar-03 5:57 
GeneralRe: CListCtrl and ImageList Pin
RobJones29-Mar-03 6:46
RobJones29-Mar-03 6:46 
GeneralRe: CListCtrl and ImageList Pin
JamesLaing29-Mar-03 8:47
JamesLaing29-Mar-03 8:47 
GeneralRe: CListCtrl and ImageList Pin
Gary R. Wheeler29-Mar-03 13:40
Gary R. Wheeler29-Mar-03 13:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.