How many 'ones complement' machines exist?
The most well-known are 30-40 years old by now and predate ANSI C (=c89), some predate even K&R C. They also have 18- or 36-bit words and other oddities. Compare that to non-ASCII systems (e.g. AS/400), which are still much in use now and probably have a sizable bit of C/C++ programs running on them (besides COBOL and Java). If you're programming for one of the more common platforms (i.e., x86, x64, ARM, PowerPC, 68k…
What is a safest way to set all bits of a variable to true in C?
21–30 of 30 posts
Re: What is a safest way to set all bits of a variable to true in C?
#22That makes me very surprised by (1) the number of up-votes, and (2) the green "check" mark of approval.
Re: What is a safest way to set all bits of a variable to true in C?
#23The "limits.h" (ISO C99 Standard) defines UINT_MAX as the maximum value for a variable of type unsigned int which is 4294967295 (0xffffffff).
Sigh : "The contents of the header are given below, in alphabetical order. The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign." #define UINT_MAX 65535 Key here is implementation-defined , with a minimum of 65535.
I always liked the definition of true in Forth (all bits set to 1). It really made it a lot easier.
Re: What is a safest way to set all bits of a variable to true in C?
#24But but but, this doesn't answer the question!? It explicitly acknowledges that -1 will not always set all bits to one, yet it recommends it! That makes me very surprised by (1) the number of up-votes, and (2) the green "check" mark of approval.
unsigned int foo = -1;
will set foo to 0xFFFF..., automatically setting all bits to 1 regardless of the number of bits in int types and without respect to the representation of negative numbers.Re: What is a safest way to set all bits of a variable to true in C?
#25But but but, this doesn't answer the question!? It explicitly acknowledges that -1 will not always set all bits to one, yet it recommends it! That makes me very surprised by (1) the number of up-votes, and (2) the green "check" mark of approval.
But but but, you apparently didn't understand the answer. It doesn't matter what the representation of -1 is. The C standard defines the cast of a negative number to an unsigned int as the (UINT_MAX + 1) modulo of the number. By definition , unsigned int foo = -1; will set foo to 0xFFFF..., automatically setting all bits to 1 regardless of the number of bits in int types and without respect to the representation of n…
Weird bit of arcana. Below is my mistaken comment. (Notice that I pretended that UINT_MAX is not all 1s, which is silly. I suppose I made that mistake because I "couldn't be wrong" or something.)
As far as I know, your definition can't be inferred from the C standard. The answer itself acknowledges that -1 doesn't yield 0xFFFF… on every platform. The only guarantee is that it will yield UINT_MAX, which is not what was asked.
Otherwise, that would mean that C basically mandates a two's complement representation. Does it?
Re: What is a safest way to set all bits of a variable to true in C?
#26The next question: Why? (To clarify: I mean, "why do you care what the bits are set to".)
I'm not sure what you mean. If you meant "why worry about bits, you should be dealing with values", then there are plenty of cases where that isn't true. Lots of programs (e.g. embedded programs) have to deal with actual bits, not with the values themselves. Just as an example, flags. (I don't know if this is what you meant, so apologies if I misunderstood your question.)
Re: What is a safest way to set all bits of a variable to true in C?
#27The "limits.h" (ISO C99 Standard) defines UINT_MAX as the maximum value for a variable of type unsigned int which is 4294967295 (0xffffffff).
Re: What is a safest way to set all bits of a variable to true in C?
#28Personally I'd use: unsigned int a = 0xFFFF, not unsigned int a = -1. This is a classic case of a readability issue. 0xFFFF is, in my mind, much clearer on your intention than -1. The only problem is that you're assuming a specific int size, but really, if you're working with bits, chances are good that you're working on a platform where you know the architecture size (at least on embedded platforms).
Re: What is a safest way to set all bits of a variable to true in C?
#29Re: What is a safest way to set all bits of a variable to true in C?
#30Personally I'd use: unsigned int a = 0xFFFF, not unsigned int a = -1. This is a classic case of a readability issue. 0xFFFF is, in my mind, much clearer on your intention than -1. The only problem is that you're assuming a specific int size, but really, if you're working with bits, chances are good that you're working on a platform where you know the architecture size (at least on embedded platforms).