Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to increment/decrement Hexadecimal value in C

Tags:

c

hex

increment

I'm trying to create a counter using hex, I know this can easily be done using decimals, but is it possible to do this in hex, or is it the same in dec?

will it go like this?

myhex = 0x00;

myhex++;

or will it be done on an entirely different manner?

If you are asking why hex its because this is for an MCU and for me the best way to control the IO of MCU is using hex.

like image 471
magicianiam Avatar asked Nov 29 '14 14:11

magicianiam


1 Answers

Yes if you try it you will see it, that it makes no difference if the number is hex, octal or decimal!

As an example:

#include <stdio.h>

int main() {

    int myhex = 0x07;
    int myOct = 07;
    int myDec = 7;

    printf("Before increment:\n");
    printf("Hex: %x\n", myhex);
    printf("Oct: %o\n", myOct);
    printf("Dec: %d\n", myDec);

    myhex++;
    myOct++;
    myDec++;

    printf("After increment:\n");
    printf("Hex: %x\n", myhex);
    printf("Oct: %o\n", myOct);
    printf("Dec: %d\n", myDec);


  return 0;

}

Output:

Before increment:
Hex: 7
Oct: 7
Dec: 7

After increment:
Hex: 8
Oct: 10
Dec: 8
like image 152
Rizier123 Avatar answered Oct 31 '22 20:10

Rizier123