This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Avoiding macro using 32 bits shift warning

Hi,

I use below macro
#define SFRX(Sfr,Msk) (Sfr=((Sfr & (~ ((Msk)>>8)) ) |(Msk)))
to simply both Set and Reset specified bits in some register.

If I write SFRX ( MyReg , 0x0100 ) this macro clears bit 0.
If I write SFRX ( MyReg , 0x0002 ) this macro set bit 1.
If I write SFRX ( MyReg , 0x0102 ) this macro clears bit 0 and sets bit 1 in one go whithout affecting other bits.

Now I would like to use this macro for a 32-bits system:
#define SREG(Reg,Msk) (Sfr=((Sfr & (~ ((Msk)>>32)) ) |(Msk)))

Although the compiler warns that shifting is too long it works fine.
(main.c(56): warning: #63-D: shift count is too large)

My question is: how do I prevent this warning to occur?

Thanks

Henk

Parents
  • [Ignoring whether it's a good idea or not.]

    To avoid the warning you want to make sure the type you are shifting (Msk) is 64-bit.

    To use 32-bit Sfr and 64-bit Msk I think you want something like:

    #define SFRX(Sfr,Msk) (Sfr = \ 
      Sfr & \ 
      (~ (unsigned int)((unsigned long long)Msk)>>32)) | \ 
      (unsigned int)Msk)
    

    You can use '__int64' instead of 'long long' if you're using a strict mode that disallows 'long long'.

    [By the way, it's not the preprocessor that is doing the math.]

Reply
  • [Ignoring whether it's a good idea or not.]

    To avoid the warning you want to make sure the type you are shifting (Msk) is 64-bit.

    To use 32-bit Sfr and 64-bit Msk I think you want something like:

    #define SFRX(Sfr,Msk) (Sfr = \ 
      Sfr & \ 
      (~ (unsigned int)((unsigned long long)Msk)>>32)) | \ 
      (unsigned int)Msk)
    

    You can use '__int64' instead of 'long long' if you're using a strict mode that disallows 'long long'.

    [By the way, it's not the preprocessor that is doing the math.]

Children
No data