Friday, October 14, 2011

Some tricks for C/C++

Over the years I've been into coding, I've found many techniques coders use to squeeze out more time out of a program or decrease their coding time. Some of them have been given here, for reference.

Use #define as function:
#define isCaps(i) (i>='A" && i<='Z')
int main()
{
    if (isCaps('S')) printf("S is in Caps\n");
    return 0;
}
Shorten your simple for loops:
#define FOR(i, a, b) for(i=a ; i<b ; i++)
int main()
{
    int i;
    FOR(i, 1, 985)
    {
        printf("%d\n",i);
    }
    return 0;
}
Test whether a number is even or odd:
#define isEven(j) (!(j & 0x01))
Divide a number by 2:
n = n >> 1;
You can find some nice bit tricks in this link. [ Bit twiddling hacks ]
I'll add more when I get these more organized :p.

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...