Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Standard way of getting a bit pattern of all ones

Is there a strictly conforming way to get a bit-pattern of all ones for an integer in C, without relying on implementation-defined behavior?

On many architectures I can expect -1 and the UINT_MAX family to give such a bit pattern; how portable is this assumption?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Extremely portable. The C23 standard disallows integer representations that are not 2’s complement, so then it will be guaranteed portable.

~0 or ~0U should always work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading