0

I'm using int, char, and unsigned int.

If I do a int foo = 1; for example, does it always create foo as 0x00...00001 ? whatever its length is, or is it only working for unsigned types?

This is because I'm doing comparisons of bits.

4
  • 4
    Before the debates will appear - are you aware what is endianness? Apr 2, 2014 at 15:22
  • 2
    Other than endianness you are going to find different representations for negative numbers.
    – pmg
    Apr 2, 2014 at 15:24
  • yes @AndrejsCainikovs Apr 2, 2014 at 15:25
  • 1
    It is not guaranteed by the standard even for unsigned types. There could be parity bits fir example. However, as long as you don't play with casts, these bits are invisible. Apr 2, 2014 at 15:36

4 Answers 4

4

This question is somewhat ill-defined. 1 (decimal) is by definition 0x00...001 (hexadecimal).

You mention "comparison of bits", so I assume you're really asking this:

Is it guaranteed that foo & (1 << n) will only evaluate to true for n == 0?".

If so, the answer is "yes".

2

you will get lots of complicated answers about what the standard actually says about bit represerntation etc. But the real answer is: Yes, 1 is always 0x000000.... 1. with size - 1 zero bits, where size is however many bits there are in the type you are using.

Note that the length of types is not defined by the C standard but you can easily find out for your platform. ie is int 16,24,32,64 bits.

and as somebody rightly says - what about endianness. This can either be totally invisible to you or bang in your face depending on what you are doing and how lucky you are (if you are on a big endian platform all the time you may never be aware of what endianness is about). If you are doing bit operations in C then you can ignore it (>> << & | ~ etc). If you are doing operations on items > 8 bits by storing then as one thing (say as an array of bytes) then reading them back as int, short etc then you will have issues.

1

since 1 is positive it will be represented as 0x0000...00001 only. but in memory how it will be stored depends on how the bytes are stored. read about Big-endian ang little-endian systems to know about storing of bytes in memory.

1

From the horse's mouth

6.2.6.2 Integer types

1 For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N−1 , so that objects of that type shall be capable of representing values from 0 to 2N − 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.53)

2 For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then MN). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

— the corresponding value with sign bit 0 is negated (sign and magnitude);
— the sign bit has the value −(2M) (two’s complement);
— the sign bit has the value −(2M − 1) (ones’ complement).

Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for ones’ complement), is a trap representation or a normal value. In the case of sign and magnitude and ones’ complement, if this representation is a normal value it is called a negative zero.
53) Some combinations of padding bits might generate trap representations, for example, if one padding bit is a parity bit. Regardless, no arithmetic operation on valid values can generate a trap representation other than as part of an exceptional condition such as an overflow, and this cannot occur with unsigned types. All other combinations of padding bits are alternative object representations of the value specified by the value bits.

What this means in practice is that the signed positive value 1 should have the low-order value bit set to 1 and all other value bits set to 0. I've never worked on a system where there were padding bits (at least any that I had to take into account).

As others have pointed out, you'll have to be aware of endianness when doing bitwise operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.