Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #14118

Re: Shifting bits

From Stanley Rice <heconghui@gmail.com>
Newsgroups comp.lang.c
Subject Re: Shifting bits
Date 2011-11-30 18:11 -0800
Organization http://groups.google.com
Message-ID <26103744.99.1322705488646.JavaMail.geo-discussion-forums@pruu5> (permalink)
References <4ed46774$0$295$14726298@news.sunsite.dk> <9jj8coF4d0U31@mid.individual.net> <0.b4c330cce627929bae11.20111129173912GMT.87fwh6rfpb.fsf@bsb.me.uk> <4ed5c4db$0$292$14726298@news.sunsite.dk>

Show all headers | View raw


On Wednesday, November 30, 2011 1:53:31 PM UTC+8, arnuld wrote:
> > On Tue, 29 Nov 2011 17:39:12 +0000, Ben Bacarisse wrote:
> 
> > Ian Collins <ian-...@hotmail.com> writes: <snip>
> >> [...]  I believe sifting a signed integer and this the code is
> >> undefined.
> > 
> > That's a good shorthand, since it will encourage the use of unsigned
> > ints wherever possible, but it is not true as you have stated it.  1<<4
> > is a shift of a signed int and is well-defined.  The exact rules can be
> > found in the C standard (search for n1256.pdf) or in any good C text,
> > but it's better to forget them!  Use unsigned ints for bit operations.
> 
> I have H&s5 here. Section 7.6.3 states "Applying the shift operator >> is 
> not portable when the left operand is negative, signed value and the 
> right operand is nonzero"
> 
> What I noticed are two things:
> 
>  (1) My examlpes uses << rather than >>. Hence above rule does not apply
>  (2) H&S5 is using word "and" rather than word "or", which directly means 
> all three conditions in "negative, signed value and right operand is 
> zero" must be satisfied.
> 
> 
> n1256.pdf in Section 6.5.7 (4) states:
> 
>   "The result of E1 << E2 is E1 left shifted E2 but positions; vacated 
> bits are filled with zeroes. If E1 has unsigned type, the result is 
> E1x2^E2, reduced modulo one more than the maximum value representable in 
> the result type. If E1 has a signed type and nonnegative value, and 
> E1x2^E2 is representable in the result type, then that is resulting 
> value; otherwise , the behavior is undefined"
> 
> It is proved that this interview code has undefined behavior. 
> (unfortunately it was a MCQ (Multiple Choice Question) and undefined 
> behavior was not one of the options).
> 
>   
> 
> > The OP's code:
> > 
> >   printf("%x\n", -1<<4);
> > 
> > is probably undefined for two reasons (the other being the technical
> > detail that %x expects an unsigned int argument) and writing
> 
> hey.. I did not know this %x secret :)
> 
> 
>  
> >   printf("%x\n", (unsigned)-1<<4);
> > 
> > would have fixed both.  Who sets these interview questions?
>  
> Yes, that casting fixes as per standard says. Thanks for that. Regarding 
> interview questions, once I was asked the value of i after this operation:
> 
>  i = i++;
> 
> I said its UB but the guy did not like my answer.
> 

Does UB stands for undefined behavior?
> 
> 
> 
> -- 
>  arnuld
> http://LispMachine.Wordpress.com

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Shifting bits arnuld <sunrise@invalid.address> - 2011-11-29 05:02 +0000
  Re: Shifting bits Ian Collins <ian-news@hotmail.com> - 2011-11-29 18:20 +1300
    Re: Shifting bits Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-29 17:39 +0000
      Re: Shifting bits arnuld <sunrise@invalid.address> - 2011-11-30 05:53 +0000
        Re: Shifting bits James Kuyper <jameskuyper@verizon.net> - 2011-11-30 06:57 -0500
        Re: Shifting bits Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-30 18:31 +0200
          Re: Shifting bits Ralf Damaschke <rwspam@gmx.de> - 2011-12-01 14:41 +0000
            Re: Shifting bits lawrence.jones@siemens.com - 2011-12-01 13:58 -0500
        Re: Shifting bits Stanley Rice <heconghui@gmail.com> - 2011-11-30 18:11 -0800
          Re: Shifting bits Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-01 02:21 -0800
  Re: Shifting bits Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-29 03:03 -0800
  Re: Shifting bits "BartC" <bc@freeuk.com> - 2011-11-29 12:04 +0000
    Re: Shifting bits 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-29 05:44 -0800
    Re: Shifting bits arnuld <sunrise@invalid.address> - 2011-11-30 05:58 +0000
  Re: Shifting bits gwowen <gwowen@gmail.com> - 2011-11-29 08:21 -0800
  Re: Shifting bits DDD <1983ddd@gmail.com> - 2011-11-29 20:42 -0800
    Re: Shifting bits James Kuyper <jameskuyper@verizon.net> - 2011-11-30 00:11 -0500
      Re: Shifting bits Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-30 18:23 +0200
        Re: Shifting bits Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-01 02:23 -0800
  Re: Shifting bits 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-30 05:35 -0800
    Re: Shifting bits Seebs <usenet-nospam@seebs.net> - 2011-11-30 18:43 +0000
      Re: Shifting bits 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-30 13:33 -0800
        Re: Shifting bits Seebs <usenet-nospam@seebs.net> - 2011-11-30 21:41 +0000
          Re: Shifting bits 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-30 14:40 -0800
            Re: Shifting bits Seebs <usenet-nospam@seebs.net> - 2011-11-30 22:36 +0000
              Re: Shifting bits Lauri Alanko <la@iki.fi> - 2011-12-01 02:02 +0000
                Re: Shifting bits Lowell Gilbert <lgusenet@be-well.ilk.org> - 2011-12-01 09:30 -0500
              Re: Shifting bits Kaz Kylheku <kaz@kylheku.com> - 2011-12-01 02:50 +0000
                Re: Shifting bits Lowell Gilbert <lgusenet@be-well.ilk.org> - 2011-12-01 09:00 -0500
      Re: Shifting bits Robert Wessel <robertwessel2@yahoo.com> - 2011-11-30 17:09 -0600
    Re: Shifting bits Stanley Rice <heconghui@gmail.com> - 2011-11-30 18:13 -0800
      Re: Shifting bits Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-11-30 19:44 -0700
      Re: Shifting bits Keith Thompson <kst-u@mib.org> - 2011-11-30 19:20 -0800
      Re: Shifting bits Seebs <usenet-nospam@seebs.net> - 2011-12-01 20:08 +0000
      Re: Shifting bits Kaz Kylheku <kaz@kylheku.com> - 2011-12-01 21:34 +0000
  Re: Shifting bits gordonb.slo24@burditt.org (Gordon Burditt) - 2011-12-01 01:24 -0600

csiph-web