Prev: invalid initialization of non-const reference of type
Next: Summary of C++0x Feature Availability
From: red floyd on 16 Mar 2010 10:41 On Mar 15, 9:27 pm, "Johannes Schaub (litb)" <schaub-johan...(a)web.de> wrote: > red floyd wrote: > > On Mar 6, 6:58 am, Olaf Klein <olaf.klei...(a)googlemail.com> wrote: > >> Dear group, > > >> is it possible to initialize an unsigned variable with its maximum > >> value with the following lines? > > >> unsigned u = -1; > >> std::size_t s = -1; > > > If you're trying to get all 1's, may I suggest > > > unsigned u = ~0; > > That's a bad idea. It only works on two's complement (on one's complement, > this can have the opposite effect of initializing "u" to all-zero bits). The > following one is more reliable: > ISO/IEC 14882:2003 3.9.1/4: "Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer." -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bart van Ingen Schenau on 17 Mar 2010 01:49 On Mar 17, 2:41 am, red floyd <redfl...(a)gmail.com> wrote: > On Mar 15, 9:27 pm, "Johannes Schaub (litb)" <schaub-johan...(a)web.de> > wrote: > > > > > red floyd wrote: > > > On Mar 6, 6:58 am, Olaf Klein <olaf.klei...(a)googlemail.com> wrote: > > >> Dear group, > > > >> is it possible to initialize an unsigned variable with its maximum > > >> value with the following lines? > > > >> unsigned u = -1; > > >> std::size_t s = -1; > > > > If you're trying to get all 1's, may I suggest > > > > unsigned u = ~0; > > > That's a bad idea. It only works on two's complement (on one's complement, > > this can have the opposite effect of initializing "u" to all-zero bits). The > > following one is more reliable: > > ISO/IEC 14882:2003 3.9.1/4: > "Unsigned integers, declared unsigned, shall obey the laws of > arithmetic modulo 2^n > where n is the number of bits in the value representation of that > particular size of integer." And how does that quote pertain to the value of the expression `~0`? It is an operation on signed integers. Bart v Ingen Schenau -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 3 4 Prev: invalid initialization of non-const reference of type Next: Summary of C++0x Feature Availability |