![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| License issues roil Linux developer community - GCN.com | iBot | UNIX and Linux RSS News | 0 | 06-17-2007 03:54 PM |
| Retrieve 5th Field to Last Field !! | jobbyjoseph | UNIX for Dummies Questions & Answers | 3 | 05-16-2007 12:20 AM |
| Moving Part of a field to another field using AWK | rjsha1 | Shell Programming and Scripting | 5 | 08-04-2006 02:39 AM |
| Linux Redhat ES 4.0 - DNS Config Issues | AbhijithS | Linux | 11 | 08-04-2006 12:04 AM |
| add increment field when first field changes | azekry | Shell Programming and Scripting | 2 | 11-14-2005 01:21 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
Bit field issues in Linux
edit: skip this post...the next is a better explaination of my issue.
Last edited by DreamWarrior; 04-07-2006 at 02:37 PM. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
OK...so, I did some more tinkering...here's some code and output and I'm very confused by what I'm seeing:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef union
{
unsigned char raw;
struct
{
unsigned char high:4;
unsigned char low:4;
} data;
} bitwiseStruct;
static void prnBits(void *addr, size_t s);
void main()
{
int i;
bitwiseStruct bws;
unsigned char c;
for (i = 0; i < 256; i++)
{
bws.raw = (unsigned char) i;
printf("For value %3d: bits [", (int) bws.raw);
prnBits(&bws.raw, sizeof(char));
c = bws.data.high;
printf("]; high [");
prnBits(&c, sizeof(char));
c = bws.data.low;
printf("]; low [");
prnBits(&c, sizeof(char));
printf("]\n");
}
}
static void prnBits(void *addr, size_t s)
{
int i, j;
unsigned char *a = (unsigned char *) addr;
for (i = 0; i < s; i++)
{
for (j = 7; j >= 0; j--)
{
printf("%c", *a & 1 << j ? '1' : '0');
}
a++;
}
}
Code:
For value 240: bits [11110000]; high [00001111]; low [00000000] Code:
For value 240: bits [11110000]; high [00000000]; low [00001111] edit: I guess it actually doesn't change the bit ordering within the byte. However, the HP compiler is extracting high and low order bits as I'd expect while the Linux compiler is swapping the extraction. Are they allowed to do this? Last edited by DreamWarrior; 04-07-2006 at 02:37 PM. |
|
#3
|
|||
|
|||
|
OK...I found out that bitfields aren't guarenteed to be packed in any way. Its implementation defined and so I'm screwed
|
|||
| Google The UNIX and Linux Forums |