Sponsored Content
Full Discussion: Bit field issues in Linux
Top Forums Programming Bit field issues in Linux Post 302070817 by DreamWarrior on Friday 7th of April 2006 04:59:59 PM
Old 04-07-2006
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 06:37 PM..
 

8 More Discussions You Might Find Interesting

1. Gentoo

compiling 32 bit application on 64 bit linux(x86_64)

hi all, i have a 64 bit linux machine. $uname -a Linux SVRDELLD30 2.6.9-42.ELsmp #1 SMP Tue Aug 15 10:35:26 BST 2006 x86_64 x86_64 x86_64 GNU/Linux here by default gcc creates 64 bit executable. but for some reason i want to create 32bit executable. first i want to create 32 bit object... (3 Replies)
Discussion started by: uttamhoode
3 Replies

2. Red Hat

boot the 32 bit kernel on a 64 bit PPC Linux machine?

Hi all, I'm looking to cover a corner case for an upcoming test cycle. Is there a way to boot a RedHat Advanced Server 4 (update 3) installed on a Power PC machine to use a 32 bit kernel? This would be similar to what is done here -> https://www.unix.com/aix/26204-aix-platform.html I've done... (0 Replies)
Discussion started by: philrau
0 Replies

3. Red Hat

Installing orca in Linux 5.2 64 bit

Hello All, We have a new project in place that calls for RHEL 5.2 64 bit version. This is our first 5.2 as well as 64 bit exposure. On our other Solaris and RHEL 4 systems we used a stats gathering software called ORCA. We are running into problems however trying to install it on RHEL 5.2 64... (0 Replies)
Discussion started by: dchitus
0 Replies

4. Red Hat

32bit Linux vs 64 bit Linux

Friends , Would u plz tell me what is the difference between 32-bit Linux o/s and the 64 bit Linux o/s . Is there any RAM limitation in this two types of o/s . Waiting for kind reply ... ... (7 Replies)
Discussion started by: shipon_97
7 Replies

5. UNIX for Advanced & Expert Users

migrating unix mp-ras 32 bit to linux suse 64 bit

Hi. I need to migrate the whole unix environment from a Unix mp-ras 32 bit to a Linux Suse 64 bit. 1) can i use cpio to copy the data? 2) can i just copy the users from unix to linux or do i have to create them by hand 3) are there any other concerns i should worry about? thanx (1 Reply)
Discussion started by: mrodrig
1 Replies

6. Windows & DOS: Issues & Discussions

Windows 7 64 bit issues with X11

Hi, I was hoping that somebody could assist me with the following issue. I recently installed a windows 7 64 bit professional on a desktop with following specs: Intel Core i7 3.4Ghz, 16 GB DDR3 RAM, AMD Radeon HD 6450. (Which was a upgrade of a xp 32 bit based system) Since I'm more... (10 Replies)
Discussion started by: kristofvdo
10 Replies

7. High Performance Computing

AVX for Linux (32-bit)

For Intel processors we've a lot of new instruction sets (AVX and AVX2 already exists, AVX512 is announced). Therefore an application has to check during run time which instruction sets are available. I've written for that purpopse some procedures, which are callable from C or C++. A strange... (2 Replies)
Discussion started by: GG2014
2 Replies

8. Programming

SQL issues comparing Long field to sysdate

I am having hard time with this sql: select partition_name, high_value FROM user_tab_partitions WHERE table_name = 'WNP_TPRESPONSE_INSTANCE' and to_char(high_value) <= to_char(sysdate - 15, 'yyyymm') ; I get an error: ORA-00932: inconsistent datatypes: expected CHAR got LONG... (1 Reply)
Discussion started by: mrn6430
1 Replies
PAM_ISSUE(8)							 Linux-PAM Manual						      PAM_ISSUE(8)

NAME
pam_issue - PAM module to add issue file to user prompt SYNOPSIS
pam_issue.so [noesc] [issue=issue-file-name] DESCRIPTION
pam_issue is a PAM module to prepend an issue file to the username prompt. It also by default parses escape codes in the issue file similar to some common getty's (using x format). Recognized escapes: d current day l name of this tty m machine architecture (uname -m) machine's network node hostname (uname -n) o domain name of this system release number of operating system (uname -r) current time s operating system name (uname -s) u number of users currently logged in U same as u except it is suffixed with "user" or "users" (eg. "1 user" or "10 users") v operating system version and build date (uname -v) OPTIONS
noesc Turns off escape code parsing. issue=issue-file-name The file to output if not using the default. MODULE TYPES PROVIDED
Only the auth module type is provided. RETURN VALUES
PAM_BUF_ERR Memory buffer error. PAM_IGNORE The prompt was already changed. PAM_SERVICE_ERR A service module error occurred. PAM_SUCCESS The new prompt was set successfully. EXAMPLES
Add the following line to /etc/pam.d/login to set the user specific issue at login: auth optional pam_issue.so issue=/etc/issue SEE ALSO
pam.conf(5), pam.d(5), pam(8) AUTHOR
pam_issue was written by Ben Collins <bcollins@debian.org>. Linux-PAM Manual 09/19/2013 PAM_ISSUE(8)
All times are GMT -4. The time now is 08:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy