Selecting bits with Unions.... Not working; please help!!
Hi, I'm struggling with selecting bits using structures inside unions.
I'm trying to convert an array of integers into a format suited to output on various chip pins.
As it stands, I have 8 integers that I want to output to a number of demultiplexer ICs. Each IC requires a 3-bit input. These inputs need to come from a chip with 3 8-bit outputs. The problem therefore is that I need to convert these ints to 3-bit format, string them together to form a 24-bit long word, and then cut that word into 8-bit long chunks for output.
This is what I've got so far, but as you can see, the bits don't seem to only partially reflect the correct values. Any idea what's wrong?
http://sharebee.com/4c6bd1d8 <-- .c file
Any help would be greatly appreciated!
======================================...
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int short x;
unsigned short led[8] = {1,1,1,1,1,1,1,1};
// PRINTS INTEGER ARRAY
printf("\nLed int array\n========================\n");
for (x=0;x<=7;x++) printf("Led %d = %d\n",x,led[x]);
printf("\n");
// UNION OF 3-BIT NUMBERS AND INDIVIDUAL BITS
union {
struct mini_words //24 bytes?
{
unsigned short a:3;
unsigned short b:3;
unsigned short c:3;
unsigned short d:3;
unsigned short e:3;
unsigned short f:3;
unsigned short g:3;
unsigned short h:3;
} word;
struct bytes
{
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
unsigned char b8:1;
unsigned char b9:1;
unsigned char b10:1;
unsigned char b11:1;
unsigned char b12:1;
unsigned char b13:1;
unsigned char b14:1;
unsigned char b15:1;
unsigned char b16:1;
unsigned char b17:1;
unsigned char b18:1;
unsigned char b19:1;
unsigned char b20:1;
unsigned char b21:1;
unsigned char b22:1;
unsigned char b23:1;
unsigned char b24:1;
} b;
} un;
// FILLING STRUCTURE WITH DATA FROM ARRAY
un.word.a=(led[0]);
un.word.b=(led[1]);
un.word.c=(led[2]);
un.word.d=(led[3]);
un.word.e=(led[4]);
un.word.f=(led[5]);
un.word.g=(led[6]);
un.word.h=(led[7]);
// PRINTING CURRENT STATE OF 3-BIT WORDS IN UNION
printf("Assignments to structure\n========================\n");
printf(" %hd ",un.word.a);
printf("%hd ",un.word.b);
printf("%hd ",un.word.c);
printf("%hd ",un.word.d);
printf("%hd ",un.word.e);
printf("%hd ",un.word.f);
printf("%hd ",un.word.g);
printf("%hd\n\n",un.word.h);
printf("Bits in string\n========================\n");
printf("%d",un.b.b0);
printf("%d",un.b.b1);
printf("%d ",un.b.b2);
printf("%d",un.b.b3);
printf("%d",un.b.b4);
printf("%d ",un.b.b5);
printf("%d",un.b.b6);
printf("%d",un.b.b7);
printf("%d ",un.b.b8);
printf("%d",un.b.b9);
printf("%d",un.b.b10);
printf("%d ",un.b.b11);
printf("%d",un.b.b12);
printf("%d",un.b.b13);
printf("%d ",un.b.b14);
printf("%d",un.b.b15);
printf("%d",un.b.b16);
printf("%d ",un.b.b17);
printf("%d",un.b.b18);
printf("%d",un.b.b19);
printf("%d ",un.b.b20);
printf("%d",un.b.b21);
printf("%d",un.b.b22);
printf("%d",un.b.b23);
getch();
return(0);
}