I know the C and C++ standards don't dictate a particular representation for numbers (could be two's complement, sign-and-magnitude, etc.). But I don't know the standards well enough (and couldn't find if it's stated) to know if there are any particular restrictions/guarantees/reserved representations made when working with bits. Particularly:
- If all the bits in an integer type are zero, does the integer as whole represent zero?
- If any bit in an integer type is one, does the integer as a whole represent non-zero? (if this is a "yes" then some representations like sign-and-magnitude would be additionally restricted)
- Is there a guaranteed way to check if any bit is not set?
- Is there a guaranteed way to check if any bit is set? (#3 and #4 kind of depend on #1 and #2, because I know how to set, for example the 5th bit (see #5) in some variable
x
, and I'd like to check a variabley
to see if it's 5th bit is 1, I would like to know ifif (x & y)
will work (because as I understand, this relies on the value of the representation and not whether nor not that bit is actually 1 or 0)) - Is there a guaranteed way to set the left-most and/or right-most bits? (At least a simpler way than taking a
char c
with all bits true (set byc = c | ~c
) and doingc = c << (CHAR_BIT - 1)
for setting the high-bit andc = c ^ (c << 1)
for the low-bit, assuming I'm not making any assumptions I should't be, given these questions) - If the answer to #1 is "no" how could one iterate over the bits in an integer type and check if each one was a 1 or a 0?
I guess my overall question is: are there any restrictions/guarantees/reserved representations made by the C and C++ standards regarding bits and integers, despite the fact that an integer's representation is not mandated (and if the C and C++ standards differ in this regard, what's their difference)?
I came up with these questions while doing my homework which required me to do some bit manipulating (note these aren't questions from my homework, these are much more "abstract").
Edit: As to what I refer to as "bits," I mean "value forming" bits and am not including "padding" bits.