473,756 Members | 4,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Architecture independant byte operations

I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant and
bitwise operations are architecture dependant. The intention is to
store two bytes, as chars, extracted from a short input parameter as:

<code>
void foo(short id_pair)
{
char *ptr = &id_pair;
memcpy(&::id1, ptr, 1);
memcpy(&::id2, prt+1, 1);
}
</code>

What is a better, architecture-independant, method of doing this?

Aug 30 '06 #1
6 3662
ca******@gmail. com wrote:
I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant and
bitwise operations are architecture dependant. The intention is to
store two bytes, as chars, extracted from a short input parameter as:

<code>
void foo(short id_pair)
{
char *ptr = &id_pair;
I assume your compiler gives you a warning here.
memcpy(&::id1, ptr, 1);
memcpy(&::id2, prt+1, 1);
Why not just assign them?
}
</code>

What is a better, architecture-independant, method of doing this?
You have to correct for the byte order of the machine.

--
Ian Collins.
Aug 30 '06 #2
On 29 Aug 2006 19:20:43 -0700, ca******@gmail. com wrote in
comp.lang.c:
I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant and
bitwise operations are architecture dependant. The intention is to
store two bytes, as chars, extracted from a short input parameter as:

<code>
void foo(short id_pair)
{
char *ptr = &id_pair;
memcpy(&::id1, ptr, 1);
The two colons above are either a syntax error or a different
language. If the latter is the case, I suggest you ask about your
assumptions in a newsgroup for that different language.

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 30 '06 #3
ca******@gmail. com wrote:
I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant and
bitwise operations are architecture dependant.
Quite the opposite, pointer arithmetic accesses data directly by
location in RAM and by definition how an object is stored in RAM is
architecture dependent.

Maths operations on the other hand, which bitwise operations are a part
of, are defined by the C standard to be specifically architecture
independent.
<code>
void foo(short id_pair)
{
char *ptr = &id_pair;
memcpy(&::id1, ptr, 1);
memcpy(&::id2, prt+1, 1);
}
</code>

What is a better, architecture-independant, method of doing this?
Use bitwise operations:

id1 = id_pair & 0xff;
id2 = (id_pair >8) & 0xff;

Aug 30 '06 #4
ca******@gmail. com wrote:
I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant and
bitwise operations are architecture dependant. The intention is to
store two bytes, as chars, extracted from a short input parameter as:
Assuming abit about the sizes of a short, how about just;

unsigned char vals[2];
unsigned short i = 0x1234;

vals[0] = i&0xff;
vals[1] = i>>8&0xff;
Aug 30 '06 #5
carsonbj posted:
><code>
void foo(short id_pair)
{
char *ptr = &id_pair;

If your compiler compiles that without complaining about a type mismatch,
then it's not a C compiler.

memcpy(&::id1, ptr, 1);
memcpy(&::id2, prt+1, 1);
The twin colons specify the "global namespace" in C++. They're a syntax error
in C.
}
</code>
--

Frederick Gotham
Aug 30 '06 #6
On 30 Aug 2006 02:49:39 -0700, "sl*******@yaho o.com"
<sl*******@gmai l.comwrote:
ca******@gmail. com wrote:
I have an issue where the below operation works on a little-endian
architecture but not on a big-endian architecture. I was under the
impression that pointer arithmetic is architecture independant and
bitwise operations are architecture dependant.

Quite the opposite, pointer arithmetic accesses data directly by
location in RAM and by definition how an object is stored in RAM is
architecture dependent.
Right. Or rather, type punning using pointer conversion as the OP did.
Pointer arithmetic on pointers of (or precisely to) one type within an
array _of that type_ is architecture independent (except that the
maximum _size_ of such an array is implementation dependent).
Maths operations on the other hand, which bitwise operations are a part
of, are defined by the C standard to be specifically architecture
independent.
Not entirely. Some aspects, like shifts of negative signed numbers and
overflow in signed (or non-IEEE floating) arithmetic, and sizes/ranges
of (most) types, are left to varying extents to the implementation.
<code>
void foo(short id_pair)
{
char *ptr = &id_pair;
memcpy(&::id1, ptr, 1);
memcpy(&::id2, prt+1, 1);
}
</code>

What is a better, architecture-independant, method of doing this?

Use bitwise operations:

id1 = id_pair & 0xff;
id2 = (id_pair >8) & 0xff;
To be absolutely safe, cast the second one to unsigned short, or
(probably better) make the parameter unsigned short to start with.

On an implementation with short and int both 16 bits, as is allowed
though nowadays pretty rare, if the value of id_pair is negative the
result of the shift is implementation-defined; in practice it is
either the result of a hardware unsigned/logical shift or a hardware
2sC-sign-propagating shift, and either way after masking is correct,
but the standard doesn't actually guarantee this.

- David.Thompson1 at worldnet.att.ne t
Sep 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
2
1796
by: andy.dreistadt | last post by:
Hi all, I came across another problem that is probably pretty easy but, again, due to my rusty-ness with C, I'm a little stumped. I have a struct that looks like this: /* Instrument Data structure */ struct instrument_info {
19
2114
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that addresses larger words only? Shouldn't sizeof be defined to return the smallest number of 'storage units' required to store an object of the type of its operand? As a general point, is there a guide to what aspects of C would fail if run on a...
14
1978
by: Champika Nirosh | last post by:
Hi All, We have a windows from application written in C#, there we have used Browser COM and other basic libraries present in standard .NET/C# SDK. So the next part is to make this appliaction available to Unix and Mac platform. I have couple of questions here 1. What are the possibilities we have of doing some thing like this? 2. What are the technical chalenges we will face of doing this? 3. What are the direct indirect solution we...
0
891
by: Trebek | last post by:
Hello all: We have a serviced comp that is called by our webservice. This component is responsible for updating various db tables as well as formatting the results of the client's request . This has worked very well for us over the last two years because client requests have always been designed to be completed async. For the first time, management wants us to offer our services in both async and sync. Obviously this will be a...
4
2437
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is unfortunate that i have to walk via System.String to do that. For instance, from an integer of value 123, i want:
5
1603
by: Olaf Baeyens | last post by:
I have another problem, maybe it is simple to fix. I have this: byte Test=new byte; But I now want to have a second pointer Test2 to point to a location inside this Test. But with no copying. Something like this.
4
1400
by: Heath Kelly | last post by:
I need advice on correct usage of ADO.NET in an ASP.Net environment. I have an ASP.Net application that accesses data through a referenced class library. Things start to break down when multiple web clients attempt to use the application at the same time. A common error that gets returned is "sqlcommand is currently busy open, fetching". I can't understand why the users might be using the same instance of my connection, but this error...
20
2516
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I have unsigned char x = 1; is it always true that (x << 1) == 2 (x << 2) == 4 etc?
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.