Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   The strangest problem.... (http://www.velocityreviews.com/forums/t315678-the-strangest-problem.html)

 Jannick 10-09-2003 10:18 AM

The strangest problem....

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 10-09-2003 11:12 AM

Re: The strangest problem....

"Jannick" <Jan@janse.dk> wrote in message
news:bm3cko\$qan\$1@news.net.uni-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 10-09-2003 11:14 AM

Re: The strangest problem....

"Jannick" <Jan@janse.dk> schrieb im Newsbeitrag
news:bm3cko\$qan\$1@news.net.uni-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 10-09-2003 11:20 AM

Re: The strangest problem....

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

"Jannick" <Jan@janse.dk> wrote in message
news:bm3cko\$qan\$1@news.net.uni-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 10-09-2003 12:27 PM

Re: The strangest problem....

In <bm3cko\$qan\$1@news.net.uni-c.dk> "Jannick" <Jan@janse.dk> 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: Dan.Pop@ifh.de

 pete 10-09-2003 12:47 PM

Re: The strangest problem....

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 10-09-2003 02:16 PM

Re: The strangest problem....

"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
news:3F8558D9.2165@mindspring.com...
> 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 10-09-2003 03:18 PM

Re: The strangest problem....

In <3f856d52\$0\$32136\$91cee783@newsreader02.highway.te lekom.at> "Robert Stankowic" <pcdoktor@netway.at> writes:

>"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
>news:3F8558D9.2165@mindspring.com...
>> 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: Dan.Pop@ifh.de

 pete 10-09-2003 11:49 PM

Re: The strangest problem....

Dan Pop wrote:
>
> In <3f856d52\$0\$32136\$91cee783@newsreader02.highway.te lekom.at> "Robert Stankowic" <pcdoktor@netway.at> writes:
>
> >"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
> >news:3F8558D9.2165@mindspring.com...
> >> 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 10-10-2003 02:58 AM

Re: The strangest problem....

"Dan Pop" <Dan.Pop@cern.ch> schrieb im Newsbeitrag
news:bm3u7i\$12l\$3@sunnews.cern.ch...
> In <3f856d52\$0\$32136\$91cee783@newsreader02.highway.te lekom.at> "Robert

Stankowic" <pcdoktor@netway.at> writes:
>
>
> >"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
> >news:3F8558D9.2165@mindspring.com...
> >> 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

All times are GMT. The time now is 06:04 PM.