473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

casting to char*,

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
8 1563
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
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
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
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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
19
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long*...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
2
by: Alex Vinokur | last post by:
classes that have virtual methods hold pointer to virtual table as additional implicit data member. So, sizeof of such classes is sizeof of all data (as struct-POD) plus 4. It seems that use of...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
24
by: Francine.Neary | last post by:
I've read that you should always cast the argument you pass to isupper(), isalnum(), etc. to unsigned char, even though their signature is int is...(int). This confuses me, for the following...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.