Assignment without using the C library

J

James H. Newman

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.
 
W

Walter Roberson

James said:
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.)
 
A

Army1987

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).
 
J

junky_fellow

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.
 
E

Even

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;
 
C

Charlie Gordon

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.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top