* union to get parts of integer
@ 2010-12-20 10:40 ratheesh k
2010-12-20 12:03 ` Reza Hoorfar
2010-12-20 14:44 ` Michal Nazarewicz
0 siblings, 2 replies; 6+ messages in thread
From: ratheesh k @ 2010-12-20 10:40 UTC (permalink / raw)
To: linux-c-programming
typedef struct {
char parts[4];
} node ;
int i=0x12345678
((node *)&i)->parts[0];
((node *)&i)->parts[1];
((node *)&i)->parts[2];
((node *)&i)->parts[3];
Is there any mechanism to split into bytes using the power of union ?
-Ratheesh
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: union to get parts of integer
2010-12-20 10:40 union to get parts of integer ratheesh k
@ 2010-12-20 12:03 ` Reza Hoorfar
2010-12-20 14:44 ` Michal Nazarewicz
1 sibling, 0 replies; 6+ messages in thread
From: Reza Hoorfar @ 2010-12-20 12:03 UTC (permalink / raw)
To: linux-c-programming
union MyUnion{
int myInt;
char myChar[sizeof(int)];
};
void main()
{
MyUnion m;
m.myInt = 0x01020304;
printf("value :%d\n",m.myChar[0]); // -> value : 4
printf("value :%d\n",m.myChar[1]); // -> value : 3
printf("value :%d\n",m.myChar[2]); // -> value : 2
printf("value :%d\n",m.myChar[3]); // -> value : 1
}
-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org] On Behalf Of ratheesh k
Sent: Monday, December 20, 2010 2:10 PM
To: linux-c-programming@vger.kernel.org
Subject: union to get parts of integer
typedef struct {
char parts[4];
} node ;
int i=0x12345678
((node *)&i)->parts[0];
((node *)&i)->parts[1];
((node *)&i)->parts[2];
((node *)&i)->parts[3];
Is there any mechanism to split into bytes using the power of union ?
-Ratheesh
--
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: union to get parts of integer
2010-12-20 10:40 union to get parts of integer ratheesh k
2010-12-20 12:03 ` Reza Hoorfar
@ 2010-12-20 14:44 ` Michal Nazarewicz
2010-12-20 15:30 ` Tim Walberg
1 sibling, 1 reply; 6+ messages in thread
From: Michal Nazarewicz @ 2010-12-20 14:44 UTC (permalink / raw)
To: ratheesh k; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 807 bytes --]
ratheesh k <ratheesh.ksz@gmail.com> writes:
> typedef struct {
> char parts[4];
> } node ;
>
> int i=0x12345678
>
> ((node *)&i)->parts[0];
> ((node *)&i)->parts[1];
> ((node *)&i)->parts[2];
> ((node *)&i)->parts[3];
>
> Is there any mechanism to split into bytes using the power of union ?
The above should work (assuming of course that sizeof(int) == 4). The
following should work as well:
((char *)&i)[0].
However, are you sure that you need this? Don't you need "(i & 255)",
"((i >> 8) & 255)", etc. instead?
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: union to get parts of integer
2010-12-20 14:44 ` Michal Nazarewicz
@ 2010-12-20 15:30 ` Tim Walberg
2010-12-20 15:40 ` Michal Nazarewicz
2010-12-21 16:22 ` Glynn Clements
0 siblings, 2 replies; 6+ messages in thread
From: Tim Walberg @ 2010-12-20 15:30 UTC (permalink / raw)
To: Michal Nazarewicz; +Cc: ratheesh k, linux-c-programming
It's likely that either the union or direct pointer expressions yield
more efficient code, as they can probably be compiled to direct byte-width
load/store instructions, rather than shifts and logical ands... However,
if the code's not in a critical path for performance, it probably won't
matter. And on some architectures, there may not be byte-width operations,
I guess...
On 12/20/2010 15:44 +0100, Michal Nazarewicz wrote:
>>
>> ((char *)&i)[0].
>>
>> However, are you sure that you need this? Don't you need "(i & 255)",
>> "((i >> 8) & 255)", etc. instead?
>>
>> --
>> Best regards, _ _
>> .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
>> ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
>> ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
End of included message
--
+----------------------+
| Tim Walberg |
| 830 Carriage Dr. |
| Algonquin, IL 60102 |
| twalberg@comcast.net |
+----------------------+
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: union to get parts of integer
2010-12-20 15:30 ` Tim Walberg
@ 2010-12-20 15:40 ` Michal Nazarewicz
2010-12-21 16:22 ` Glynn Clements
1 sibling, 0 replies; 6+ messages in thread
From: Michal Nazarewicz @ 2010-12-20 15:40 UTC (permalink / raw)
To: Tim Walberg; +Cc: ratheesh k, linux-c-programming
Tim Walberg <twalberg@comcast.net> writes:
> It's likely that either the union or direct pointer expressions yield
> more efficient code, as they can probably be compiled to direct byte-width
> load/store instructions, rather than shifts and logical ands... However,
> if the code's not in a critical path for performance, it probably won't
> matter. And on some architectures, there may not be byte-width operations,
> I guess...
It's not the issue of performance. It's an issue of whether you want
each separate byte as saved in memory (which should rarely be the case)
or if you want specific part of the value.
> On 12/20/2010 15:44 +0100, Michal Nazarewicz wrote:
>>>
>>> ((char *)&i)[0].
>>>
>>> However, are you sure that you need this? Don't you need "(i & 255)",
>>> "((i >> 8) & 255)", etc. instead?
>>>
>>> --
>>> Best regards, _ _
>>> .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
>>> ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
>>> ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: union to get parts of integer
2010-12-20 15:30 ` Tim Walberg
2010-12-20 15:40 ` Michal Nazarewicz
@ 2010-12-21 16:22 ` Glynn Clements
1 sibling, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2010-12-21 16:22 UTC (permalink / raw)
To: Tim Walberg; +Cc: Michal Nazarewicz, ratheesh k, linux-c-programming
Tim Walberg wrote:
> >> ((char *)&i)[0].
> >>
> >> However, are you sure that you need this? Don't you need "(i & 255)",
> >> "((i >> 8) & 255)", etc. instead?
>
> It's likely that either the union or direct pointer expressions yield
> more efficient code, as they can probably be compiled to direct byte-width
> load/store instructions, rather than shifts and logical ands... However,
> if the code's not in a critical path for performance, it probably won't
> matter. And on some architectures, there may not be byte-width operations,
> I guess...
The cast/union versions will give different results depending upon the
CPU's byte order. The shift/mask versions will give the same results
regardless of byte order.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2010-12-21 16:22 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-20 10:40 union to get parts of integer ratheesh k
2010-12-20 12:03 ` Reza Hoorfar
2010-12-20 14:44 ` Michal Nazarewicz
2010-12-20 15:30 ` Tim Walberg
2010-12-20 15:40 ` Michal Nazarewicz
2010-12-21 16:22 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).