Velocity Reviews > The strangest problem....

# The strangest problem....

Jannick
Guest
Posts: n/a

 10-09-2003
Hi I have:
unsigned int myvalue=0;
unsigned char mytest=0;

mytest=0x34;
myvalue = mytest<<24;

Then myvalue gets the value 0x1A000000!!!!!
How is this possible? Should'nt it be 0x34000000???

Best Regards
Jannick

Allan Bruce
Guest
Posts: n/a

 10-09-2003

"Jannick" <(E-Mail Removed)> wrote in message
news:bm3cko\$qan\$(E-Mail Removed)-c.dk...
> Hi I have:
> unsigned int myvalue=0;
> unsigned char mytest=0;
>
> mytest=0x34;
> myvalue = mytest<<24;
>
> Then myvalue gets the value 0x1A000000!!!!!
> How is this possible? Should'nt it be 0x34000000???
>
> Best Regards
> Jannick
>
>

Are you sure, its not a typo in your program? 0x1A000000 is 0x34<<23
Allan

Robert Stankowic
Guest
Posts: n/a

 10-09-2003

"Jannick" <(E-Mail Removed)> schrieb im Newsbeitrag
news:bm3cko\$qan\$(E-Mail Removed)-c.dk...
> Hi I have:
> unsigned int myvalue=0;
> unsigned char mytest=0;
>
> mytest=0x34;
> myvalue = mytest<<24;
>
> Then myvalue gets the value 0x1A000000!!!!!
> How is this possible? Should'nt it be 0x34000000???

Yes, it should (and is on my system, which of course doesn't proof
anything).

from the standard:
"4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
are filled with zeros. If E1 has an unsigned type, the value of the result
is E1 x 2^E2, reduced modulo one more than the maximum value representable
in the result type. If E1 has a signed type and nonnegative value, and E1 x
2^E2 is representable in the result type, then that is the resulting value;
otherwise, the behavior is undefined."

The only explanation I have - besides a typo (23 instead of 24) in your
actual code is something else in the code causing UB. I can't imagine, that
your compiler is broken in _such_ a way.
Can you post a minimum _compilable_ program which exhibits the error?

Robert

hongky
Guest
Posts: n/a

 10-09-2003
maybe your compiler os OS
my VC+win32 is right,
myvalue=0x34

"Jannick" <(E-Mail Removed)> wrote in message
news:bm3cko\$qan\$(E-Mail Removed)-c.dk...
> Hi I have:
> unsigned int myvalue=0;
> unsigned char mytest=0;
>
> mytest=0x34;
> myvalue = mytest<<24;
>
> Then myvalue gets the value 0x1A000000!!!!!
> How is this possible? Should'nt it be 0x34000000???
>
> Best Regards
> Jannick
>
>

Dan Pop
Guest
Posts: n/a

 10-09-2003
In <bm3cko\$qan\$(E-Mail Removed)-c.dk> "Jannick" <(E-Mail Removed)> writes:

>unsigned int myvalue=0;
>unsigned char mytest=0;
>
>mytest=0x34;
>myvalue = mytest<<24;
>
>Then myvalue gets the value 0x1A000000!!!!!
>How is this possible? Should'nt it be 0x34000000???

Show us a minimal, but *complete* program illustrating your problem.
Without seeing your code, not even my crystal ball is of much help here.
Except for suggesting that the shift count was actually 23 in your real
program.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)

pete
Guest
Posts: n/a

 10-09-2003
Jannick wrote:
>
> Hi I have:
> unsigned int myvalue=0;
> unsigned char mytest=0;
>
> mytest=0x34;
> myvalue = mytest<<24;
>
> Then myvalue gets the value 0x1A000000!!!!!
> How is this possible? Should'nt it be 0x34000000???

I guess we're all assuming that the width of unsigned,
is at least 25 bits.
Is it, on your system ?

--
pete

Robert Stankowic
Guest
Posts: n/a

 10-09-2003

"pete" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Jannick wrote:
> >
> > Hi I have:
> > unsigned int myvalue=0;
> > unsigned char mytest=0;
> >
> > mytest=0x34;
> > myvalue = mytest<<24;
> >
> > Then myvalue gets the value 0x1A000000!!!!!
> > How is this possible? Should'nt it be 0x34000000???

>
> I guess we're all assuming that the width of unsigned,
> is at least 25 bits.
> Is it, on your system ?

Well, if myvalue becomes 0x1A000000 we can assume that, can we?
And besides that, even if the width would be less than 24 the result would
be well defined and definitely not 0x1a000000
If I am not mistaken
on all sizes up to and including 26 bits the value would be 0x0,
27 and 28 bits will give 0x4000000,
29 bits 0x14000000,
30 bits and above 0x34000000

kind regards
Robert

Dan Pop
Guest
Posts: n/a

 10-09-2003
In <3f856d52\$0\$32136\$(E-Mail Removed) lekom.at> "Robert Stankowic" <(E-Mail Removed)> writes:

>"pete" <(E-Mail Removed)> schrieb im Newsbeitrag
>news:(E-Mail Removed)...
>> Jannick wrote:
>> >
>> > Hi I have:
>> > unsigned int myvalue=0;
>> > unsigned char mytest=0;
>> >
>> > mytest=0x34;
>> > myvalue = mytest<<24;
>> >
>> > Then myvalue gets the value 0x1A000000!!!!!
>> > How is this possible? Should'nt it be 0x34000000???

>>
>> I guess we're all assuming that the width of unsigned,
>> is at least 25 bits.
>> Is it, on your system ?

>
>Well, if myvalue becomes 0x1A000000 we can assume that, can we?

We can safely assume 29 bits. Which makes a minimum of 32 bits a
reasonable assumption. Pete should have engaged his brain before posting.

>And besides that, even if the width would be less than 24 the result would
>be well defined and definitely not 0x1a000000
>If I am not mistaken
>on all sizes up to and including 26 bits the value would be 0x0,
>27 and 28 bits will give 0x4000000,
>29 bits 0x14000000,
>30 bits and above 0x34000000

Wrong.

If the value of the right operand is negative or is greater than or
equal to the width in bits of the promoted left operand, the behavior
is undefined.

There was a long thread on this issue in comp.std.c, a few weeks ago.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)

pete
Guest
Posts: n/a

 10-09-2003
Dan Pop wrote:
>
> In <3f856d52\$0\$32136\$(E-Mail Removed) lekom.at> "Robert Stankowic" <(E-Mail Removed)> writes:
>
> >"pete" <(E-Mail Removed)> schrieb im Newsbeitrag
> >news:(E-Mail Removed)...
> >> Jannick wrote:
> >> >
> >> > Hi I have:
> >> > unsigned int myvalue=0;
> >> > unsigned char mytest=0;
> >> >
> >> > mytest=0x34;
> >> > myvalue = mytest<<24;
> >> >
> >> > Then myvalue gets the value 0x1A000000!!!!!
> >> > How is this possible? Should'nt it be 0x34000000???
> >>
> >> I guess we're all assuming that the width of unsigned,
> >> is at least 25 bits.
> >> Is it, on your system ?

> >
> >Well, if myvalue becomes 0x1A000000 we can assume that, can we?

>
> We can safely assume 29 bits. Which makes a minimum of 32 bits a
> reasonable assumption.
> Pete should have engaged his brain before posting.

A printf call could display 0x1A000000 with 16 bits and UB.

--
pete

Robert Stankowic
Guest
Posts: n/a

 10-10-2003

"Dan Pop" <(E-Mail Removed)> schrieb im Newsbeitrag
news:bm3u7i\$12l\$(E-Mail Removed)...
> In <3f856d52\$0\$32136\$(E-Mail Removed) lekom.at> "Robert

Stankowic" <(E-Mail Removed)> writes:
>
>
> >"pete" <(E-Mail Removed)> schrieb im Newsbeitrag
> >news:(E-Mail Removed)...
> >> Jannick wrote:
> >> >
> >> > Hi I have:
> >> > unsigned int myvalue=0;
> >> > unsigned char mytest=0;
> >> >
> >> > mytest=0x34;
> >> > myvalue = mytest<<24;
> >> >
> >> > Then myvalue gets the value 0x1A000000!!!!!
> >> > How is this possible? Should'nt it be 0x34000000???
> >>
> >> I guess we're all assuming that the width of unsigned,
> >> is at least 25 bits.
> >> Is it, on your system ?

> >
> >Well, if myvalue becomes 0x1A000000 we can assume that, can we?

>
> We can safely assume 29 bits. Which makes a minimum of 32 bits a
> reasonable assumption. Pete should have engaged his brain before posting.
>
> >And besides that, even if the width would be less than 24 the result

would
> >be well defined and definitely not 0x1a000000
> >If I am not mistaken
> >on all sizes up to and including 26 bits the value would be 0x0,
> >27 and 28 bits will give 0x4000000,
> >29 bits 0x14000000,
> >30 bits and above 0x34000000

>
> Wrong.
>
> If the value of the right operand is negative or is greater than or
> equal to the width in bits of the promoted left operand, the behavior
> is undefined.
>

So I obviously misunderstand the text from N869:

"4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits
are filled with zeros. If E1 has an unsigned type, the value of the result
is E1 x 2^E2, reduced modulo one more than the maximum value representable
in the result type. If E1 has a signed type and nonnegative value, and E1 x
2^E2 is representable in the result type, then that is the resulting value;
otherwise, the behavior is undefined."

The semicolon and the lower case "otherwise" suggested to me that the last
phrase belongs to the sentence about signed types.
Thank you for the clarification.
kind regards
Robert

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post luserXtrog C Programming 23 05-17-2009 09:22 AM KlausK Wireless Networking 5 06-16-2008 07:05 PM Neo Geshel ASP .Net 7 07-01-2005 08:51 AM Ivan Ostreš Cisco 3 02-09-2005 08:28 AM Emilio ASP .Net 1 10-15-2003 10:59 AM

Advertisments