473,404 Members | 2,174 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,404 software developers and data experts.

Assignment without using the C library

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

Similar topics

5
by: news | last post by:
I'm trying out the Zend Development Environment 4, and for the most part I like it, except for two things. It seems to think certain lines that are perfectly valid are bugs. Like this: while...
23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
6
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class...
10
by: Anon Email | last post by:
In the code below, what does this mean? mine = (short *)0; -------------- #include <iostream> int main() {
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
4
by: Pierre Barbier de Reuille | last post by:
Hello, a discussion began on python-dev about this. It began by a bug report, but is shifted and it now belongs to this discussion group. The problem I find with augmented assignment is it's...
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.