All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Nazarewicz <mina86@tlen.pl>
To: ratheesh k <ratheesh.ksz@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: union to get parts of integer
Date: Mon, 20 Dec 2010 15:44:48 +0100	[thread overview]
Message-ID: <87aak0tsf3.fsf@erwin.mina86.com> (raw)
In-Reply-To: <AANLkTikAB_-kO_cmD5VQRtztx=4V9NDcmU4uWBDE=ON8@mail.gmail.com> (ratheesh k.'s message of "Mon, 20 Dec 2010 16:10:17 +0530")

[-- 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 --]

  parent reply	other threads:[~2010-12-20 14:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2010-12-20 15:30   ` Tim Walberg
2010-12-20 15:40     ` Michal Nazarewicz
2010-12-21 16:22     ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87aak0tsf3.fsf@erwin.mina86.com \
    --to=mina86@tlen.pl \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=ratheesh.ksz@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.