c - how to mask out 'upper half' of long int -


I have a question about the creation of bitmask in C. I need to mask at least the important half part of the 'long interval', so that I only left with the upper part, I need to make sure that if I'm on a 64-bit or 32-bit platform, So this is not an outshone half . I think __WORD_SIZE is defined by limits.h. Initially I am doing this like this:

  define #UPPER (X) (x and (~ 0 <__WORDSIZE / 2))  < / Pre> 

What is the most accurate and efficient way to do this?

like

  #UPPER (x) (x & amp; (~ 0 <(sizeof (x) * 4)))  

here So far, if the limitations are not present or if __WORDSIZE is not defined for any reason, it will also work for other types, so that you are sad it is an int, a Use it on AOTA, A4, etc. Any decent compiler,

  sizeof (x) * 4  
will calculate the value

compile time ( Since they are both constants), which means that you do not have to worry about any performance.

Edit: Correct Error - Size is not in the bytes of size, so our To get the right result to multiply by 4 (8/2)

Edit 2: If you have true Uc want to be priests, then you can use it

  #define UPPER (x) (x & amp; (~ 0  

CHAR_BIT is defined in a continuous limits.h - this The number specifies the number of bits in a character, and the platform is specific, however, it is not really necessary (in general), because at the AFAIK normal usage ATM there is no platform that uses a non-standard size byte .


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -