By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,600 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,600 IT Pros & Developers. It's quick & easy.

casting to char*,

P: n/a
On Windows XP, unsigned integers are 32 bits. Partition the bits of the
unsigned integer C into 4 8-bit chunks like so:

C = XRGB.

Then

unsigned char* p = &C;

Does

p[0] = X; ?
p[1] = R; ?
p[2] = G; ?
p[3] = B; ?

or

p[0] = B; ?
p[1] = G; ?
p[2] = R; ?
p[3] = X; ?
Sep 15 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Since you are on a little-endian machine, I believe the answer is #2.

On Fri, 15 Sep 2006, vsgdp wrote:
On Windows XP, unsigned integers are 32 bits. Partition the bits of the
unsigned integer C into 4 8-bit chunks like so:

C = XRGB.

Then

unsigned char* p = &C;

Does

p[0] = X; ?
p[1] = R; ?
p[2] = G; ?
p[3] = B; ?

or

p[0] = B; ?
p[1] = G; ?
p[2] = R; ?
p[3] = X; ?
Sep 15 '06 #2

P: n/a
vsgdp posted:
On Windows XP, unsigned integers are 32 bits.

Depends on your compiler.

Partition the bits of the unsigned integer C into 4 8-bit chunks like
so:

C = XRGB.

Then

unsigned char* p = &C;

Does

p[0] = X; ?
p[1] = R; ?
p[2] = G; ?
p[3] = B; ?

or

p[0] = B; ?
p[1] = G; ?
p[2] = R; ?
p[3] = X; ?

Depends how much crack you smoke, because your code has no basis in C++.

--

Frederick Gotham
Sep 16 '06 #3

P: n/a
On Windows XP, unsigned integers are 32 bits. Partition the bits of the
unsigned integer C into 4 8-bit chunks like so:

C = XRGB.

Then

unsigned char* p = &C;

Does

p[0] = X; p[1] = R; p[2] = G; p[3] = B; ?
or
p[0] = B; p[1] = G; p[2] = R; p[3] = X; ?
Easy enough to check:

unsigned int C = 1;
cout << sizeof(C) << endl;
unsigned char* p = (unsigned char*)&C;
cout << "[0]" << (int)p[0] << "\t";
cout << "[1]" << (int)p[1] << "\t";
cout << "[2]" << (int)p[2] << "\t";
cout << "[3]" << (int)p[3] << endl;

For option 1, the expected output would be
4
[0]0 [1]0 [2]0 [3]1
For option 2:
4
[0]1 [1]0 [2]0 [3]0

(Further testing with C = 256, C = 256 * 256, and C = 256*256*256 would
add to the confidence you get running this.)

Michael

P.S. On my machine, with my compiler, I get option 2.

Sep 16 '06 #4

P: n/a
On Sat, 16 Sep 2006 00:19:27 GMT, Frederick Gotham
<fg*******@SPAM.comwrote in comp.lang.c++:
vsgdp posted:
On Windows XP, unsigned integers are 32 bits.


Depends on your compiler.

Partition the bits of the unsigned integer C into 4 8-bit chunks like
so:

C = XRGB.

Then

unsigned char* p = &C;

Does

p[0] = X; ?
p[1] = R; ?
p[2] = G; ?
p[3] = B; ?

or

p[0] = B; ?
p[1] = G; ?
p[2] = R; ?
p[3] = X; ?


Depends how much crack you smoke, because your code has no basis in C++.
Perhaps you should "read over" your posts, as you claim to "read over"
your code, with the exception of that which you post to usenet.

The OP's code is 100% strictly conforming C, based on the fact that
the implementation has an exact-width 32-bit unsigned integer type
with no padding bits, and CHAR_BIT is 8. The OP specified the former
(and it is true), and while he did not specify the latter, it is true
as well.

Given that those two conditions are satisfied, his code is perfectly
correct, although the result is implementation-defined. Any object in
C++, and I do mean any object, not just PODs, may be examined as an
array of unsigned chars.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 16 '06 #5

P: n/a
Frederick Gotham <fg*******@SPAM.comwrites:
vsgdp posted:
On Windows XP, unsigned integers are 32 bits.


Depends on your compiler.
Uh? On SysV ABI compliant system such as Linux it doesn't. Are you
sure that in the Windows world, there is no such standard?

Can anyone shed light on this?

Jens
Sep 16 '06 #6

P: n/a
Jens Theisen wrote:
Frederick Gotham <fg*******@SPAM.comwrites:
>vsgdp posted:
>>On Windows XP, unsigned integers are 32 bits.


Depends on your compiler.

Uh? On SysV ABI compliant system such as Linux it doesn't. Are you
sure that in the Windows world, there is no such standard?
There is not.

MS publishes an API for system calls, but have no requirements for
apps (or compilers).
Bo Persson
Sep 16 '06 #7

P: n/a
"Bo Persson" <bo*@gmb.dkwrites:
There is not.

MS publishes an API for system calls, but have no requirements for
apps (or compilers).
Clearly a dll and the code that's calling into it must have the same
idea of what's an unsigned int.

So I gather this API they publish is authorative for any compiler
vendor.

Regards,

Jens
Sep 16 '06 #8

P: n/a
"vsgdp" <he***@null.comwrote in message
news:lSGOg.9585$cw.4804@fed1read03...
On Windows XP, unsigned integers are 32 bits. Partition the bits of the
unsigned integer C into 4 8-bit chunks like so:
C = XRGB.
Then
unsigned char* p = &C;
Does
p[0] = X; ?
p[1] = R; ?
p[2] = G; ?
p[3] = B; ?
or
p[0] = B; ?
p[1] = G; ?
p[2] = R; ?
p[3] = X; ?
Why don't you try it and find out? That will tell you what your particular
platform does.

In practice, though, I would avoid writing this kind of code. Instead, I
would look at the problem this way:

What exactly do you mean when you say

C = XRGB

? I am going to assume that if you write

C = 0x01020304;

then you intend for X to be 1, R to be 2, G to be 3, and B to be 4. In that
case, you can write the following:

B = C & 0xff;
C >>= 8;
G = C & 0xff;
C >>=8;
R = C & 0xff;
C >>= 8;
X = C & 0xff;

and you will not have to worry about how your particular platform behaves.

Before you claim that this code will be slower than yours, please measure
it. Your version requires 4 memory references (5 if C is not already in
memory); mine requires none.


Sep 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.