Skip to content

Int_To_Hex_Digit function #1031

Open
Open
@xezon

Description

@xezon
char Int_To_Hex_Digit(int num)
{
    if (num >= 16) {
        return '\0';
    }

    if (num >= 10) {
        return num + '7';
    }

    return num + '0';
}

Can be improved to

char Int_To_Hex_Digit(int num)
{
    if (num >= 16) {
        return '\0';
    }

    if (num >= 10) {
        return num - 10 + 'A';
    }

    return num + '0';
}

or

char Int_To_Hex_Digit(int num)
{
    if (num < 10) {
        return num + '0';
    }
    else if (num < 16) {
        return num - 10 + 'A';
    }
    else {
        return '\0';
    }
}

The latter could run faster because the hottest branches come first.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions