473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Question: About bit-field storage.

Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
And when I use the same code to print an interger, it works well.

Here is my code and its output, can you give me some advice? Thank
you.

output of my code:
Print bit-field structure:
56 ffffff80 0 0
Print interger:
78 56 34 12

Code:

#include <stdio.h>

struct bit_test {
int a:4,
b:8,
c:20;
};

int main(void)
{
int i, testi;
struct bit_test test;
char *p;

test.a = 6;
test.b = 5;
test.c = 8;

p = (char *)&test;
printf("Print bit-field structure:\n");
for (i = 0; i < sizeof(struct bit_test); i++) {
printf("%x ", p[i]);
}
printf("\n");
testi = 0x12345678;
p = (char *)&testi;
printf("Print interger:\n");
for (i = 0; i < sizeof(int); i++) {
printf("%x ", p[i]);
}

printf("\r\n");
return 0;
}

Nov 16 '07 #1
3 2158
quemix wrote On 11/16/07 10:37,:
Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
Since you do not know how `test' is laid out (that is
what you are trying to discover, after all), why do you
think the result is "wrong?"
And when I use the same code to print an interger, it works well.
[... code snipped ...]
Two suggestions:

1) Try the integer-printing code with a different value,
like 0x87654321.

2) Add `#include <limits.h>' at the beginning of your
program, and add `printf ("%d %d\n", CHAR_MIN, CHAR_MAX);'
somewhere within the executed part.

--
Er*********@sun.com
Nov 16 '07 #2

Eric Sosman wrote:
Two suggestions:

1) Try the integer-printing code with a different value,
like 0x87654321.

2) Add `#include <limits.h>' at the beginning of your
program, and add `printf ("%d %d\n", CHAR_MIN, CHAR_MAX);'
somewhere within the executed part.
Thanks Chris and Eric.

When I replace char with unsigned char, only one byte is printed (no ffffff, which
is my purpose). I think the data in memory is same, and just the way to
interpret it changed.
Nov 17 '07 #3
On Fri, 16 Nov 2007 23:37:41 +0800, quemix <qu****@gmail.comwrote:
>Hi all:
I have defined a structure (bit field) like this:

struct bit_test {
int a:4,
b:8,
c:20;
};
struct bit_test test;

To know the real memory storage of "test", I print every byte of it.
But surprisingly, the result is wrong: 56 ffffff80 0 0.
And when I use the same code to print an interger, it works well.

Here is my code and its output, can you give me some advice? Thank
you.

output of my code:
Print bit-field structure:
56 ffffff80 0 0
Print interger:
78 56 34 12

Code:

#include <stdio.h>

struct bit_test {
int a:4,
b:8,
c:20;
};

int main(void)
{
int i, testi;
struct bit_test test;
char *p;
Try unsigned char.
>
test.a = 6;
test.b = 5;
test.c = 8;

p = (char *)&test;
And here
printf("Print bit-field structure:\n");
for (i = 0; i < sizeof(struct bit_test); i++) {
printf("%x ", p[i]);
}
printf("\n");
testi = 0x12345678;
p = (char *)&testi;
And here
printf("Print interger:\n");
for (i = 0; i < sizeof(int); i++) {
printf("%x ", p[i]);
}

printf("\r\n");
return 0;
}



Remove del for email
Nov 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: aaj | last post by:
This is a simple question compared to some of the stuff that gets asked (and answered) here, but sometimes its easy to over look the simpler things. I've been working with databases for a few...
1
by: Kurt Krueckeberg | last post by:
In the book C++ Gothcas, Gotcha #7 is an example of using boolean logic to simply code. My question follows this snippet from the book. "Do you have to count to eight when presented with the...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
19
by: Randy Yates | last post by:
Consider the following code: #include "dsptypes.h" /* definitions */ #define VECTOR_LENGTH 64 /* local variables */
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
3
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address...
2
by: Frank Rizzo | last post by:
Hello, I have a pretty nice box in production (2 dual core Opterons) and it is running the 32-bit version of Windows Server 2003 and 32-bit version of SQL Server 2000. The application that's...
6
by: Jhon | last post by:
Hi every one, I got very basic question, here i go: Say i have 11001 11010 bits which are infact 10 bits. Now i want to address every bit so if it is zero i would add one and if it is one...
6
by: BillG | last post by:
I have the following defined. enum DisplayEventType{DISPUTE = 0x0001, ACTION = 0x0002, QUESTION = 0x0004); DisplayEventType dEventType; if(DisputesCheckBox.Checked) dEventType = dEventType...
6
by: GaryDean | last post by:
I recently transferred a web project from my 32 bit computer running server2003 to my 64bit Vista Ultimate computer. Of course all of my dlls, etc are 32 bit but everything ran fine on the new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.