All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ben Rosenberg <scdlbx@gmail.com>
To: Randi Botse <nightdecoder@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Help on bit operation
Date: Wed, 26 Aug 2009 16:45:22 -0700	[thread overview]
Message-ID: <ca4725240908261645xff91d02nfa52ae22a5190c17@mail.gmail.com> (raw)
In-Reply-To: <34e1241d0908261602k4d07d422o7ea69b606210701a@mail.gmail.com>

On Wed, Aug 26, 2009 at 4:02 PM, Randi Botse<nightdecoder@gmail.com> wrote:
> Hi, I'm beginner C programmer, i have a problem, i want to store some
> information in a integer, a integer will be 32 bit on my machine, i
> want to have as follow:
>
> 6 bit (information 1)   MSB
> 4 bit (information 2)
> 8 bit (information 3)
> 5 bit (information 4)
> 9 bit (information 5)   LSB
>
> For example i set the informations as follow (in decimal):
>
> information 1 = 43        or 101011
> information 2 = 11        or 1011
> information 3 = 120      or 1111000
> information 3 = 30        or 11110
> information 4 = 418      or 110100010
>
> if i join all informations i should get a 32 bit integer valued
> 2935782212 or 01010111011111100011110110100010, then my problem is how
> to retrieve these informations on bit operation? i want to know what's
> the value of information-2 or information-3, etc directly.  And, is
> there any good way to join these informations to be an 32 bit integer?
>
> at this time i convert the 32bit integer into binary string, process
> it's with array segment to get all informations then convert them to
> integer,
> to build the 32bit integer, i join all information value into binary
> string (yes, 32 bit of char ;p) join all of them then convert to
> integer.
>
> I know my way is sucks and too far away from COOL thing ;p,  i think
> there are cool way to do this!.
>
> Thanks before!
> --
> 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
>

A quick, dirty way of doing it without structs:

#include <stdio.h>

void store_in_int( unsigned int *target, unsigned int offset, size_t
field_sz, unsigned int value ) {
    unsigned int mask = ((1<<(field_sz))-1)<<offset;
    /* Clear field */
    *target |= mask;
    *target ^= mask;
    /* Load field */
    *target |= value<<offset;
}

unsigned int retrieve_from_int( unsigned int target, unsigned int
offset, size_t field_sz ) {
    unsigned int mask = ((1<<(field_sz))-1);
    return (target>>offset)&mask;
}

int main ( void ) {
    unsigned int info=0;
    store_in_int(&info, 0, 6, 43);
    store_in_int(&info, 6, 4, 11);
    store_in_int(&info,10, 8,120);
    store_in_int(&info,18, 5, 30);
    store_in_int(&info,23, 9,418);
    printf("info1: %u\n",retrieve_from_int( info, 0, 6 ));
    printf("info2: %u\n",retrieve_from_int( info, 6, 4 ));
    printf("info3: %u\n",retrieve_from_int( info,10, 8 ));
    printf("info4: %u\n",retrieve_from_int( info,18, 5 ));
    printf("info5: %u\n",retrieve_from_int( info,23, 9 ));
    return 0;
}
--
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

  parent reply	other threads:[~2009-08-26 23:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-26 23:02 Help on bit operation Randi Botse
2009-08-26 23:33 ` Akos Marton
2009-08-26 23:37 ` Uday Verma
2009-08-26 23:45 ` Ben Rosenberg [this message]
2009-08-27  0:15 ` Glynn Clements
2009-08-27  6:43   ` Randi Botse
2009-08-28  4:52     ` Randi Botse
2009-08-28 17:02       ` Ben Rosenberg
     [not found]         ` <34e1241d0908281139k66e8102dy94b70e9fcfd86763@mail.gmail.com>
2009-08-28 18:40           ` Randi Botse
2009-08-28 19:58             ` Ben Rosenberg
2009-08-28 20:14               ` Tim Walberg
2009-08-28 20:19             ` Glynn Clements
2009-08-29  7:40               ` Randi Botse

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=ca4725240908261645xff91d02nfa52ae22a5190c17@mail.gmail.com \
    --to=scdlbx@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nightdecoder@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.