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

Assignment without using the C library

P: n/a
I have the following:

typedef unsigned char T[2] ;

T y = { 0x22, 0x33 } ;

I now define

unsigned short x ;

My question is, can one write an assignment statement such that the two
bytes pointed to by y are copied to x?

Let me first preempt a couple of objections. First, for my
purposes unsigned char is one byte long and unsigned short is two bytes
long. Second, I know how to make the assignment by means of memcpy(), but
I wonder if there is a way of doing it without using the C library, or
putting in place what would amount to a C implementation of memcpy()?

What I have in mind is some casting magic that would do it in a
single assignment statement.
Sep 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
James H. Newman wrote:
I have the following:

typedef unsigned char T[2] ;

T y = { 0x22, 0x33 } ;

I now define

unsigned short x ;

x = *(unsigned short *)(&y);
Sep 12 '07 #2

P: n/a
In article <46***********************@news.orange.fr>,
jacob navia <ja***@jacob.remcomp.frwrote:
>James H. Newman wrote:
> I have the following:
> typedef unsigned char T[2] ;
> T y = { 0x22, 0x33 } ;
>I now define
> unsigned short x ;
> x = *(unsigned short *)(&y);
Tempting, but unsigned char T[2] is not necessarily aligned
according to the alignment restrictions for unsigned short.

You can use that kind of cast if you -know- the objects have
the proper alignment, such as if they belong to a union of
the two types, or if the space is at the beginning of a
malloc()'d area (malloc returns memory aligned suitably for
all possible types.)
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Sep 12 '07 #3

P: n/a
On Wed, 12 Sep 2007 17:58:01 +0000, James H. Newman wrote:
I have the following:

typedef unsigned char T[2] ;

T y = { 0x22, 0x33 } ;

I now define

unsigned short x ;

My question is, can one write an assignment statement such that the two
bytes pointed to by y are copied to x?
[...]
What I have in mind is some casting magic that would do it in a
single assignment statement.
No cast needed:
x = y[0] << CHAR_BIT | y[1]; (for big endian), or
x = y[1] << CHAR_BIT | y[0]; (for little endian).

--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 12 '07 #4

P: n/a
On Sep 13, 1:18 am, Army1987 <army1...@NOSPAM.itwrote:
On Wed, 12 Sep 2007 17:58:01 +0000, James H. Newman wrote:
I have the following:
typedef unsigned char T[2] ;
T y = { 0x22, 0x33 } ;
I now define
unsigned short x ;
My question is, can one write an assignment statement such that the two
bytes pointed to by y are copied to x?
[...]
What I have in mind is some casting magic that would do it in a
single assignment statement.

No cast needed:
x = y[0] << CHAR_BIT | y[1]; (for big endian), or
x = y[1] << CHAR_BIT | y[0]; (for little endian).
I think,
x=y[0] << CHAR_BIT | y[1];
will work for both big endian and little endian.
And
x = y[1] << CHAR_BIT | y[0];
will fail to work on either big or little endian.

Sep 13 '07 #5

P: n/a
On Sep 13, 1:58 am, "James H. Newman" <NewJa...@exicite.comwrote:
I have the following:

typedef unsigned char T[2] ;

T y = { 0x22, 0x33 } ;

I now define

unsigned short x ;

My question is, can one write an assignment statement such that the two
bytes pointed to by y are copied to x?

Let me first preempt a couple of objections. First, for my
purposes unsigned char is one byte long and unsigned short is two bytes
long. Second, I know how to make the assignment by means of memcpy(), but
I wonder if there is a way of doing it without using the C library, or
putting in place what would amount to a C implementation of memcpy()?

What I have in mind is some casting magic that would do it in a
single assignment statement.
Try the following:
union t2s{
T t;
unsigned short yy;
}ts;
ts.t[0]=y[0];
ts.t[1]=y[1];
ts.yy=x;

Sep 13 '07 #6

P: n/a
<ju**********@yahoo.co.ina crit dans le message de news:
11**********************@y42g2000hsy.googlegroups. com...
On Sep 13, 1:18 am, Army1987 <army1...@NOSPAM.itwrote:
>On Wed, 12 Sep 2007 17:58:01 +0000, James H. Newman wrote:
I have the following:
typedef unsigned char T[2] ;
T y = { 0x22, 0x33 } ;
I now define
unsigned short x ;
My question is, can one write an assignment statement such that the two
bytes pointed to by y are copied to x?
[...]
What I have in mind is some casting magic that would do it in a
single assignment statement.

No cast needed:
x = y[0] << CHAR_BIT | y[1]; (for big endian), or
x = y[1] << CHAR_BIT | y[0]; (for little endian).

I think,
x=y[0] << CHAR_BIT | y[1];
will work for both big endian and little endian.
And
x = y[1] << CHAR_BIT | y[0];
will fail to work on either big or little endian.
Well it depends what you are talking about.
Both will potentially invoke undefined behaviour on the DS9K.

hint: 16 bit ints, 8 bit chars, y[0] 127 or y[1] 127
y[0] is promoted to int, then shifted left CHAR_BIT places. This is
undefined if the operation causes overflow.

--
Chqrlie.
Sep 13 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.