The strangest problem....

J

Jannick

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
 
A

Allan Bruce

Jannick said:
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
 
R

Robert Stankowic

Jannick said:
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
 
D

Dan Pop

In said:
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
 
P

pete

Jannick said:
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 ?
 
R

Robert Stankowic

pete said:
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
 
D

Dan Pop

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
 
P

pete

Dan said:
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.
 
R

Robert Stankowic

Dan Pop said:
In <[email protected]> "Robert

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


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
 
J

Jannick

Dan Pop said:
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


Here is my code. I would actually like to do the commented operation, but
also with the code as it is it gives me the strange error :-(. The funny
thing is that when I do measuredata.n1.number1<<16 I get ram_value =
0x00340000, but when I do measuredata.n1.number1<<24 I get 0x1A000000. It
seems to me that it actually only does measuredata.n1.number1<<23....I can
seem to see why :-(

Code:

struct no1_{
unsigned char number1;
unsigned char number2;
};

struct no2_{
unsigned int number3;
unsigned int number4;
};

struct measurement_data{
struct no1_ n1;
struct no2_ n2;
}measuredata;


int main(){

unsigned int* sram_location = (unsigned int *) 0xFF120011;

measuredata.n1.number1 = 0x34;
measuredata.n1.number2 = 0xBA;
measuredata.n2.number3 = 0x12345678;
measuredata.n2.number4 = 0xABCDEF12;
mytest= 0x34;


/* *sram_location = ( ((measuredata.n1.number1)<<24) | (
(measuredata.n1.number2)<<16) | ( (measuredata.n2.number3)>>16) );*/

ram_value = measuredata.n1.number1<<24;
*sram_location = ram_value;

return 0;
}
 
D

Dan Pop

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

The probability of this happening is so close to that of nasal demon
generation that it's not worth considering in the context of a *real*
implementation.

Dan
 
D

Dan Pop

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.

The origin of your misunderstanding is the omission of reading paragraph
3 *before* reading paragraph 4. Paragraph 3 contains the text I have
quoted (actually a similar text, since I was quoting from C89).

Dan
 
D

Dan Pop

In said:
Here is my code. I would actually like to do the commented operation, but
also with the code as it is it gives me the strange error :-(. The funny
thing is that when I do measuredata.n1.number1<<16 I get ram_value =
0x00340000, but when I do measuredata.n1.number1<<24 I get 0x1A000000. It
seems to me that it actually only does measuredata.n1.number1<<23....I can
seem to see why :-(

Code:

struct no1_{
unsigned char number1;
unsigned char number2;
};

struct no2_{
unsigned int number3;
unsigned int number4;
};

struct measurement_data{
struct no1_ n1;
struct no2_ n2;
}measuredata;


int main(){

unsigned int* sram_location = (unsigned int *) 0xFF120011;
^^^^^^^^^^
This is a *very* suspicious address for an unsigned int! Are you sure
you know what you're doing?
measuredata.n1.number1 = 0x34;
measuredata.n1.number2 = 0xBA;
measuredata.n2.number3 = 0x12345678;
measuredata.n2.number4 = 0xABCDEF12;
mytest= 0x34;


/* *sram_location = ( ((measuredata.n1.number1)<<24) | (
(measuredata.n1.number2)<<16) | ( (measuredata.n2.number3)>>16) );*/

ram_value = measuredata.n1.number1<<24;
*sram_location = ram_value;

return 0;
}

Nope, this is NOT your code! It contains two undeclared identifiers,
mytest and ram_value and it generates no output, so that we can actually
see what happens. If I remove mytest, declare ram_value as unsigned int
and display its value instead of dereferencing a dubious pointer, I get
the following result:

fangorn:~/tmp 544> gcc test.c
fangorn:~/tmp 545> ./a.out
34000000

So, please don't waste our time with bogus pieces of code that neither
compile nor illustrate your problem.

Dan
 
C

CBFalconer

Jannick said:
.... snip ...

Here is my code. I would actually like to do the commented operation, but
also with the code as it is it gives me the strange error :-(. The funny
thing is that when I do measuredata.n1.number1<<16 I get ram_value =
0x00340000, but when I do measuredata.n1.number1<<24 I get 0x1A000000. It
seems to me that it actually only does measuredata.n1.number1<<23....I can
seem to see why :-(

Code:

struct no1_{
unsigned char number1;
unsigned char number2;
};

struct no2_{
unsigned int number3;
unsigned int number4;
};

struct measurement_data{
struct no1_ n1;
struct no2_ n2;
}measuredata;

int main(){

unsigned int* sram_location = (unsigned int *) 0xFF120011;

Undefined behavior here.
measuredata.n1.number1 = 0x34;
measuredata.n1.number2 = 0xBA;
measuredata.n2.number3 = 0x12345678;
measuredata.n2.number4 = 0xABCDEF12;
mytest= 0x34;

/* *sram_location = ( ((measuredata.n1.number1)<<24) | (
(measuredata.n1.number2)<<16) | ( (measuredata.n2.number3)>>16) );*/

ram_value = measuredata.n1.number1<<24;
*sram_location = ram_value;

return 0;
}

Not a complete program. No way of displaying any fault. Illegal
constructs. Why are you wasting our time with this?
 
D

Default User

CBFalconer said:
Undefined behavior here.


Mmmmm, nope.


[#5] An integer may be converted to any pointer type. |
Except as previously specified, the result is
implementation-defined, might not be properly aligned, and
might not point to an entity of the referenced type.49)


49)The mapping functions for converting a pointer to an
integer or an integer to a pointer are intended to be
consistent with the addressing structure of the execution
environment.


Assignment is ok. Use of it is problematic.



Brian Rodenborn
 
J

Jannick

Default User said:
CBFalconer said:
Undefined behavior here.


Mmmmm, nope.


[#5] An integer may be converted to any pointer type. |
Except as previously specified, the result is
implementation-defined, might not be properly aligned, and
might not point to an entity of the referenced type.49)


49)The mapping functions for converting a pointer to an
integer or an integer to a pointer are intended to be
consistent with the addressing structure of the execution
environment.


Assignment is ok. Use of it is problematic.



Brian Rodenborn

Sorry to post defective code....I was to quick in cutting the problem out of
entire code... I think it is my compiler tricking me :-(, the "unsigned int*
sram_location = (unsigned int *) 0xFF120011" is okay because the code is
intended for a microcontroller that has a peripheral device memory-mapped at
this location.

I solved the problem though, but do still not understand it...I think the
error lies in the compiler and dont think the error is easily seen. When I
did:

*sram_location = ( ((measuredata.n1.number1)<<24) | (
(measuredata.n1.number2)<<16) | ( (measuredata.n2.number3)>>16) );

I got the 0x1A000000

When I do the:
*sram_location = (
((measuredata.n1.number1<<16)<<8) | ((measuredata.n1.number2)<<16) | (
(measuredata.n2.number3) >>16) );
I get the correct result: 0x34000000

Dont ask me why......

Thank you all for your time, effort and patience.

Best Regards
Jannick
 
C

CBFalconer

Jannick said:
.... snip ...

Sorry to post defective code....I was to quick in cutting the
problem out of entire code... I think it is my compiler tricking
me :-(, the "unsigned int* sram_location = (unsigned int *)
0xFF120011" is okay because the code is intended for a
microcontroller that has a peripheral device memory-mapped at
this location.

I solved the problem though, but do still not understand it...I
think the error lies in the compiler and dont think the error is
easily seen. When I did:

If you are writing into a section of memory mapped i/o, some of
those locations are inputs. They can't be expected to store
values.
 
K

Keith Thompson

Jannick said:
Sorry to post defective code....I was to quick in cutting the problem out of
entire code... I think it is my compiler tricking me :-(, the "unsigned int*
sram_location = (unsigned int *) 0xFF120011" is okay because the code is
intended for a microcontroller that has a peripheral device memory-mapped at
this location.

I solved the problem though, but do still not understand it...I think the
error lies in the compiler and dont think the error is easily seen. When I
did:

*sram_location = ( ((measuredata.n1.number1)<<24) | (
(measuredata.n1.number2)<<16) | ( (measuredata.n2.number3)>>16) );

I got the 0x1A000000

When I do the:
*sram_location = (
((measuredata.n1.number1<<16)<<8) | ((measuredata.n1.number2)<<16) | (
(measuredata.n2.number3) >>16) );
I get the correct result: 0x34000000

Dont ask me why......

The only difference (which would have been a lot clearer if you had
formatted and indented the code more clearly) is the change from

(measuredata.n1.number1)<<24

to

(measuredata.n1.number1<<16)<<8

What happens when you display the values of those expressions in
isolation? What are the types of number1, number2, number3, and
*sram_location? What is the size of int on your platform?

If number1 is an unsigned int, and unsigned int is 16 bits on your
platform, you could be seeing undefined behavior because the right
operand of the "<<" is greater than or equal to the size of the
operand. If this is the problem, and you're trying to get an unsigned
long result (at least 32 bits), you should cast each operand to unsigned
long before shifting it:

*sram_location =
( (unsigned long)measuredata.n1.number1 << 24 ) |
( (unsigned long)measuredata.n1.number2 << 16 ) |
( (unsigned long)measuredata.n2.number3 >> 16 );

(On the other hand, if number3 is 16 bits, it doesn't make much sense
to do a 16-bit right shift on it.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top