Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Bitfields?? "shift too large" in initialziation list.

Reply
Thread Tools

Bitfields?? "shift too large" in initialziation list.

 
 
MikeWhy
Guest
Posts: n/a
 
      04-05-2011
Why shouldn't the following work? VC2008 complains "shift count negative or
too big, undefined behavior". Indeed, foo.b and foo.c contain 0, given
non-zero values.

The following prints:
8
162e (5678:0:0)
162f (5679:0:0)
cd2000000000162e (5678:1234:1)

//=====================================
// fooBitfield.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


typedef unsigned __int64 Unt64;
struct Foo
{
//=============
Unt64 a : 52;
Unt64 b : 11;
Unt64 c : 1;
};

std:stream & operator<<(std:stream & os, const Foo & r)
{
const Unt64 ival = *reinterpret_cast<const Unt64*>(&r);
os << std::hex << ival << std::dec << " ("
<< r.a << ':'
<< r.b << ':'
<< r.c << ')';
return os;
}
int main(int argc, char** argv)
{
//-- doesn't work. Prints "(5678:0:0)"
{
const Foo foo = {
(Unt64)atoi(argv[1]),
(Unt64)atoi(argv[2]),
(Unt64)atoi(argv[3])
};
std::cout << sizeof(foo) << '\n'
<< foo << '\n';
}

//-- doesn't work. Prints "(5679:0:0)"
{
//!! const Foo foo2 = { 3, 3, 3};
const Foo foo2 = { 3ULL, 3ULL, 3ULL }; // makes no difference.
std::cout << foo2 << '\n';
}

//-- works just fine.
{
Foo foo3;
foo3.a = atoi(argv[1]);
foo3.b = atoi(argv[2]);
foo3.c = atoi(argv[3]);
std::cout << foo3 << '\n';
}

return 0;
}


 
Reply With Quote
 
 
 
 
ZMZ
Guest
Posts: n/a
 
      04-06-2011
I tried in VC 2008 SP1 (32 bits),

//-- doesn't work. Prints "(5679:0:0)"
{
//!! const Foo foo2 = { 3, 3, 3};
const Foo foo2 = { 3ULL, 3ULL, 3ULL }; // makes no difference.
std::cout << foo2 << '\n';
}

It prints out (3:0:0) for me, instead of 5679:0:0.

It is probably a bug in VC. I tried the code (change _int64 to long
long) in GCC and it works fine.

Btw, in VC, if you make the bit field size smaller (smaller than 32
bits), everything within 32bits will work just fine too, only those
fields exceeding 32bits will have problem.




On Apr 6, 3:09*am, "MikeWhy" <boat042-nos...@yahoo.com> wrote:
> Why shouldn't the following work? VC2008 complains "shift count negative or
> too big, undefined behavior". Indeed, foo.b and foo.c contain 0, given
> non-zero values.
>
> The following prints:
> 8
> 162e (5678:0:0)
> 162f (5679:0:0)
> cd2000000000162e (5678:1234:1)
>
> //=====================================
> // fooBitfield.cpp : Defines the entry point for the console application.
> //
>
> #include "stdafx.h"
>
> typedef unsigned __int64 Unt64;
> struct Foo
> {
> * *//=============
> * *Unt64 a : 52;
> * *Unt64 b : 11;
> * *Unt64 c : 1;
>
> };
>
> std:stream & operator<<(std:stream & os, const Foo & r)
> {
> * *const Unt64 ival = *reinterpret_cast<const Unt64*>(&r);
> * *os << std::hex << ival << std::dec << " ("
> * * * << r.a << ':'
> * * * << r.b << ':'
> * * * << r.c << ')';
> * *return os;}
>
> int main(int argc, char** argv)
> {
> * *//-- doesn't work. Prints "(5678:0:0)"
> * *{
> * * * const Foo foo = {
> * * * * *(Unt64)atoi(argv[1]),
> * * * * *(Unt64)atoi(argv[2]),
> * * * * *(Unt64)atoi(argv[3])
> * * * };
> * * * std::cout << sizeof(foo) << '\n'
> * * * * *<< foo << '\n';
> * *}
>
> * *//-- doesn't work. Prints "(5679:0:0)"
> * *{
> * * * //!! const Foo foo2 = { 3, 3, 3};
> * * * const Foo foo2 = { 3ULL, 3ULL, 3ULL }; // makes no difference.
> * * * std::cout << foo2 << '\n';
> * *}
>
> * *//-- works just fine.
> * *{
> * * * Foo foo3;
> * * * foo3.a = atoi(argv[1]);
> * * * foo3.b = atoi(argv[2]);
> * * * foo3.c = atoi(argv[3]);
> * * * std::cout << foo3 << '\n';
> * *}
>
> * *return 0;
>
>
>
>
>
>
>
> }


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-06-2011
On 04/ 6/11 07:09 AM, MikeWhy wrote:
> Why shouldn't the following work? VC2008 complains "shift count negative
> or too big, undefined behavior". Indeed, foo.b and foo.c contain 0,
> given non-zero values.
>
> The following prints:
> 8
> 162e (5678:0:0)
> 162f (5679:0:0)
> cd2000000000162e (5678:1234:1)
>
> typedef unsigned __int64 Unt64;
> struct Foo
> {
> //=============
> Unt64 a : 52;
> Unt64 b : 11;
> Unt64 c : 1;
> };


long long isn't officially supported (yet) in C++, so how the compiler
handles long long bit fields is an implementation specific issue. Some
will refuse to compile them (Sun CC will in 32 bit mode).

You have probably hit a compiler bug.

Incidentally, Sun CC gets it wrong:

8
80b0000000000034 (52:11:1)
300000003 (12884901891:0:0)
80b0000000000034 (52:11:1)

gcc gets it right:

8
80b0000000000034 (52:11:1)
8030000000000003 (3:3:1)
80b0000000000034 (52:11:1)

and warns about 3 not fitting in a single bit.

--
Ian Collins
 
Reply With Quote
 
MikeWhy
Guest
Posts: n/a
 
      04-07-2011

"Pete Becker" <> wrote in message
news:2011040609082589691-pete@versatilecodingcom...
> On 2011-04-06 03:45:43 -0400, Ian Collins said:
>
>> On 04/ 6/11 07:09 AM, MikeWhy wrote:
>>> Why shouldn't the following work? VC2008 complains "shift count negative
>>> or too big, undefined behavior". Indeed, foo.b and foo.c contain 0,
>>> given non-zero values.
>>>
>>> The following prints:
>>> 8
>>> 162e (5678:0:0)
>>> 162f (5679:0:0)
>>> cd2000000000162e (5678:1234:1)
>>>
>>> typedef unsigned __int64 Unt64;
>>> struct Foo
>>> {
>>> //=============
>>> Unt64 a : 52;
>>> Unt64 b : 11;
>>> Unt64 c : 1;
>>> };

>>
>> long long isn't officially supported (yet) in C++, so how the compiler
>> handles long long bit fields is an implementation specific issue. Some
>> will refuse to compile them (Sun CC will in 32 bit mode).

>
> The layout of bit-fields is always implementation-specific:
>
> Allocation of bit-fields within a class object is implementation-defined.
> Alignment of bit-fields is
> implementation-defined. Bit-fields are packed into some addressable
> allocation unit.
> [ Note: bit-fields straddle allocation units on some machines and not on
> others. Bit-fields
> are assigned right-to-left on some machines, left-to-right on others. —
> end note ]
>
> 9.6 [class.bit] /1


Thanks, one and all. It was all very helpful.

 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      04-07-2011
"ZMZ" <>
>I tried in VC 2008 SP1 (32 bits),


> //-- doesn't work. Prints "(5679:0:0)"
> {
> //!! const Foo foo2 = { 3, 3, 3};
> const Foo foo2 = { 3ULL, 3ULL, 3ULL }; // makes no difference.
> std::cout << foo2 << '\n';
> }
>
>It prints out (3:0:0) for me, instead of 5679:0:0.


One field has only 1 bits and gets assigned value of 3. I'd bet that is
undefined behavior, so anything can happen. Does it work without overflow?

 
Reply With Quote
 
ZMZ
Guest
Posts: n/a
 
      04-07-2011
On Apr 7, 2:41*pm, "Balog Pal" <p...@lib.hu> wrote:
> "ZMZ" <zhangmin...@gmail.com>
>
> >I tried in VC 2008 SP1 (32 bits),
> > //-- doesn't work. Prints "(5679:0:0)"
> > *{
> > * * *//!! const Foo foo2 = { 3, 3, 3};
> > * * *const Foo foo2 = { 3ULL, 3ULL, 3ULL }; // makes no difference.
> > * * *std::cout << foo2 << '\n';
> > *}

>
> >It prints out (3:0:0) for me, instead of 5679:0:0.

>
> One field has only 1 bits and gets assigned value of 3. I'd bet that is
> undefined behavior, so anything can happen. Does it work without overflow?


No, not working.

For VC, as long as you have bit fields beyond 32bits, regardless the
number of bits and the value used to initialize it, the result will be
wrong.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Client Assembly.. what is too big or too small ??? Martin ASP .Net 0 08-04-2004 08:47 AM
Are these pictures too dark or/and too large? Luigi Donatello Asero HTML 13 05-21-2004 04:54 AM
Problems with imaging (too slow or too much RAM) SB Java 0 08-05-2003 11:06 AM
Re: Is this just too hard for folks here? Or too stupid? Mark Parnell HTML 0 06-23-2003 11:02 PM
Re: Is this just too hard for folks here? Or too stupid? Mike Foster HTML 0 06-23-2003 07:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57