Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: bit value of a negative number |
| Date | 2016-07-30 11:42 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87a8gzxv8b.fsf@bsb.me.uk> (permalink) |
| References | <159d5df7-d640-40b1-9866-2bb16d7769ee@googlegroups.com> |
sinbad <sinbad.sinbad@gmail.com> writes:
> why does the following program prints a garbage value for the last bit.
> I was expecting a '1' instead of 4294967295.
>
> 0 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 1
> 1 1 1 1 1 1 1 4294967295
>
> 1 #include"stdio.h"
> 2
> 3 int bit_mask(int x)
> 4 {
> 5 int i = 0;
> 6 int j = 0;
> 7
> 8 for (i = 0;i < 4 ;i++) {
> 9 for (j = 0;j < 8;j++) {
> 10 printf("%u ", (x & (1 << ((i*8) + j))) >> ((i*8) + j));
> 11 }
> 12 printf("\n");
> 13 }
> 14
> 15 return(0);
> 16 }
> 17
> 18 int main()
> 19 {
> 20 int x = -2;
> 21
> 22 bit_mask(x);
> 23
> 24 return(0);
> 25 }
I think most people would prefer source to shown without line numbers.
It makes it simpler to test.
Anyway, getting to your question...
In order to allow fast code to be generated, C imposes only very loose
conditions on what its operators should do. Over the years, hardware
designers have implemented lots of slightly different kinds of shift
instruction that do slightly different things in the various odd cases
like shifting a bit up into the sign position, or shifting all the bit
out of a number (shifting by the width or more). Anything outside a
very narrow common core of behaviour, C defined as "undefined". This
means that a << or a >> can be replaced by a single shift instruction
because C only guarantees the minimum that almost all hardware complies
with.
When you do 1 << 31 (that's when i == 3 and j == 7) your program is
undefined by the C standard. At this point anything goes. On an x86
the most likely outcome is you get -2147483648 (the value of 1 in the
sign bit and zeros elsewhere). Shifting that value right is, again,
undefined by C but, in this case, happens to propagate the sign bit
giving you -1 (the value when all the bit are 1). Now you do a third
undefined operation, or at least one that is dodgy, because you try to
print a signed int with the conversion used for unsigned int. This has
causes printf to treat the all-bits 1 pattern as an unsigned int whose
value is 2**32-1 or 4294967295.
Wherever possibly you should avoid writing programs with undefined
behaviour. There is really no alternative but to learn exactly what C
defines for the shift operations, but very often all you need to do is
to use unsigned arithmetic rather than signed arithmetic. Your example
can be fixed by adding one character: change the 1 to 1u.
Often the best way to examine the representation of an object is to
treat it as a sequence of unsigned char which you can do with a union.
For any complete object type T you can write
union { T value; unsigned char byte[sizeof (T)]; } u;
and, after setting u.value, you can look at u.byte[0] and upwards to see
the bits that make up the value (including any padding bits).
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
bit value of a negative number sinbad <sinbad.sinbad@gmail.com> - 2016-07-30 02:38 -0700
Re: bit value of a negative number Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-07-30 02:50 -0700
Re: bit value of a negative number Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-30 11:42 +0100
Re: bit value of a negative number James Kuyper <jameskuyper@verizon.net> - 2016-07-30 09:38 -0400
Re: bit value of a negative number "James R. Kuyper" <jameskuyper@verizon.net> - 2016-07-30 11:39 -0400
Re: bit value of a negative number Keith Thompson <kst-u@mib.org> - 2016-07-30 13:59 -0700
Re: bit value of a negative number Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-30 15:16 +0100
Re: bit value of a negative number Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-31 06:31 -0700
Re: bit value of a negative number Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-31 17:28 +0100
Re: bit value of a negative number supercat@casperkitty.com - 2016-07-31 13:19 -0700
Re: bit value of a negative number Ike Naar <ike@iceland.freeshell.org> - 2016-07-31 20:46 +0000
Re: bit value of a negative number Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-30 14:21 -0700
Re: bit value of a negative number supercat@casperkitty.com - 2016-07-31 13:37 -0700
Re: bit value of a negative number Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-31 16:59 -0700
Re: bit value of a negative number supercat@casperkitty.com - 2016-07-31 12:56 -0700
Re: bit value of a negative number Barry Schwarz <schwarzb@dqel.com> - 2016-07-30 10:15 -0700
Re: bit value of a negative number Tim Rentsch <txr@alumni.caltech.edu> - 2016-07-30 10:25 -0700
csiph-web