The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-07-2006
Registered User
 

Join Date: Oct 2003
Posts: 69
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.
Reply With Quote
Forum Sponsor
  #2  
Old 04-07-2006
Registered User
 

Join Date: Oct 2003
Posts: 69
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++;
	}
}
now, I'd expect this code to work identically on all machines. Why? I was under the impression that the endianess of a machine only affects byte ordering within a word *NOT* bit ordering within a byte. However, on a PA-Risc HP-UX box, for the value 0xF0 (240 decimal, 11110000 binary) I get the following output:

Code:
For value 240:  bits [11110000]; high [00001111]; low [00000000]
While, for my x86 based Linux box I get:

Code:
For value 240:  bits [11110000]; high [00000000]; low [00001111]
WHY?!?! This seems wrong!

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.
Reply With Quote
  #3  
Old 04-07-2006
Registered User
 

Join Date: Oct 2003
Posts: 69
OK...I found out that bitfields aren't guarenteed to be packed in any way. Its implementation defined and so I'm screwed . That sucks too because it'd otherwise be such an elegant solution. I wonder why the specification left it open, if it were guarenteed it'd make doing bit manipulation so much easier than forcing me to manually mask off everything.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0