473,219 Members | 1,637 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,219 software developers and data experts.

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 3627
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.learn.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*******@yahoo.com"
<sl*******@gmail.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.net
Sep 7 '06 #7

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
2
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...
19
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...
14
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...
0
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 . ...
4
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...
5
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...
4
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...
20
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.