Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > The strangest problem....

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      10-09-2003

"Jannick" <> wrote in message
news:bm3cko$qan$...
> 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


 
Reply With Quote
 
 
 
 
Robert Stankowic
Guest
Posts: n/a
 
      10-09-2003

"Jannick" <> schrieb im Newsbeitrag
news:bm3cko$qan$...
> 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


 
Reply With Quote
 
hongky
Guest
Posts: n/a
 
      10-09-2003
maybe your compiler os OS
my VC+win32 is right,
myvalue=0x34

"Jannick" <> wrote in message
news:bm3cko$qan$...
> 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
>
>



 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-09-2003
In <bm3cko$qan$> "Jannick" <> 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:
 
Reply With Quote
 
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
 
Reply With Quote
 
Robert Stankowic
Guest
Posts: n/a
 
      10-09-2003

"pete" <> schrieb im Newsbeitrag
news:...
> 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


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-09-2003
In <3f856d52$0$32136$ lekom.at> "Robert Stankowic" <> writes:


>"pete" <> schrieb im Newsbeitrag
>news:...
>> 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:
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-09-2003
Dan Pop wrote:
>
> In <3f856d52$0$32136$ lekom.at> "Robert Stankowic" <> writes:
>
> >"pete" <> schrieb im Newsbeitrag
> >news:...
> >> 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
 
Reply With Quote
 
Robert Stankowic
Guest
Posts: n/a
 
      10-10-2003

"Dan Pop" <> schrieb im Newsbeitrag
news:bm3u7i$12l$...
> In <3f856d52$0$32136$ lekom.at> "Robert

Stankowic" <> writes:
>
>
> >"pete" <> schrieb im Newsbeitrag
> >news:...
> >> 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



 
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
lisp toy: strangest warning I've seen luserXtrog C Programming 23 05-17-2009 09:22 AM
Strangest Wireless Problem I've Ever Seen KlausK Wireless Networking 5 06-16-2008 07:05 PM
Strangest damn error - database at fault? Neo Geshel ASP .Net 7 07-01-2005 08:51 AM
Strangest IPSec thing... Ivan Ostreš Cisco 3 02-09-2005 08:28 AM
Strangest Control behaviour ?! Emilio ASP .Net 1 10-15-2003 10:59 AM



Advertisments