Packing data...

F

FMorales

Hello all, quick hopefully easy question. Here goes.

I've started on the Art Of Assembly tutorial(s)
(http://webster.cs.ucr.edu/Page_asm/0_ArtOfAsm.html)
and got to the sample projects and kinda got stuck. I'm needing to use a
language of my choosing (which
i easily chose C) to do this project where it wants me to pack data into a
16 bit variable. Now i am
familiar with C to a pretty decent extent, just not bit manipulation. So
what i'm needing help on is getting
this data packed into an unsigned short int, data type.

The example it gives is : 0100 0001 0101 1000 or 4158h
0100 being a 4 for the month,
00010 being 2 for the day, and
1011000 being 88 for the year.

These will come into my function as 3 seperate variables.I'm using 2 single
characters for the month & day, then an array for the year.(they need to
enter
in say 1988 for the above example, and i just grab out the 88. Not a
problem).
So i would basically get something along the lines of, myFunc('4', '2',
"1988");
And i just need help getting those values in their correct "spot" packed
into my
16 bit data type. (I can get 1 of them, but after the first one i keep
messing
up the way it's packed in by trying to pack the next value in.). As i said
i'm
still learning this stuff, so i'm kinda picking up on things as i go. Any
help,
and or a comments are definatly welcome. I've been searching google and such
for
help but so far have found none. Thought i'd give here a shot. Thx in
advance all,
....Frank M...
 
N

Nick Austin

Hello all, quick hopefully easy question. Here goes.

I've started on the Art Of Assembly tutorial(s)
(http://webster.cs.ucr.edu/Page_asm/0_ArtOfAsm.html)
and got to the sample projects and kinda got stuck. I'm needing to use a
language of my choosing (which
i easily chose C) to do this project where it wants me to pack data into a
16 bit variable. Now i am
familiar with C to a pretty decent extent, just not bit manipulation. So
what i'm needing help on is getting
this data packed into an unsigned short int, data type.

The example it gives is : 0100 0001 0101 1000 or 4158h
0100 being a 4 for the month,
00010 being 2 for the day, and
1011000 being 88 for the year.

Bitfield packing is done with the left-shift operator and the
bitwise inclusive or operator:

unsigned short month = 4;
unsigned short day = 2;
unsigned short year = 88;
unsigned short dmy;

dmy = month << 12 | day << 7 | year;

However it's a good idea to add range checks and/or masking so
that one field cannot overflow into the adjacent field.
These will come into my function as 3 seperate variables.I'm using 2 single
characters for the month & day, then an array for the year.(they need to
enter
in say 1988 for the above example, and i just grab out the 88. Not a
problem).

I'm glad it's "not a problem". You can combine your solution to
this bit with the packing operation from above.

Nick.
 
P

Pieter Droogendijk

So what i'm needing help on is getting this data packed into an unsigned short
int, data type.

bitfields or shifts.
The example it gives is : 0100 0001 0101 1000 or 4158h
0100 being a 4 for the month,
00010 being 2 for the day, and
1011000 being 88 for the year.

So i would basically get something along the lines of, myFunc('4', '2',
"1988");

what about december 31 2003? myFunc ('12', '31', "03")?
And i just need help getting those values in their correct "spot" packed
into my
16 bit data type. (I can get 1 of them, but after the first one i keep
messing
up the way it's packed in by trying to pack the next value in.).

You can pack it into a data type, but it's printed value will change when you
run it on a big-endian system.

union {
unsigned short int si;
struct {
unsigned year:7;
unsigned day:5;
unsigned month:4;
} bf;
} cram;
main() {
cram.bf.month = 4;
cram.bf.day = 2;
cram.bf.year = 88;
printf ("%X\n", cram.si);
}

or if you like less readable but much shorter code:

main () {
unsigned short int sint = 0;
sint = 4<<12 | 2<<7 | 88;
printf ("%X\n", sint);
}

I'd pick the first option. It's understandable, and reading the variables is
easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,204
Latest member
abhinav72673

Latest Threads

Top