freelanceprogrammers.org Forum Index » C

[ Doubt ] Bitfields


View user's profile Post To page top
contactrrajesh@... Posted: Tue Jan 18, 2005 12:51 pm


Joined: 04 Jan 2005

Posts: 2
[ Doubt ] Bitfields
Hi All,
 
 struct st {       int i : 4;       int j : 4;       int k : 4; }st;
 
main()
{
    printf( " size = %d ", sizeof(st));
}
 
The output of the program is 4   The size of structure is sum of size of its member.  Hence  i,j,k are each 4 bits and totally 12 bits(nearly 2 bytes).  But the output is 4 bytes.  How this is possible ?? I am using Microsoft C++ compiler (integer size is 4 bytes) 
 
Thanks in Advance,
R.Rajesh.
Reply with quote
Send private message
View user's profile Post To page top
karthikvgroups Posted: Wed Jan 19, 2005 11:50 am


Joined: 19 Jan 2005

Posts: 1
[ Doubt ] Bitfields
--- In Programmers-Town@yahoogroups.com, "R.Rajesh"
<contactrrajesh@h...> wrote:
> Hi All,
>
> struct st
> {
> int i : 4;
> int j : 4;
> int k : 4;
> }st;
>
> main()
> {
> printf( " size = %d ", sizeof(st));
> }
>
> The output of the program is 4 . The size of structure is sum of
size of its member. Hence i,j,k are each 4 bits and totally 12 bits
(nearly 2 bytes). But the output is 4 bytes. How this is
possible ?? I am using Microsoft C++ compiler (integer size is 4
bytes)
>
> Thanks in Advance,
> R.Rajesh.

That is because, the unused 2 bytes also belong to the same
structure as it wouldn`t be able to allocate this memory location to
some other variable, try adding three more members with each 4 bits,
even then the size of the structure would remain 4 bytes.
Reply with quote
Send private message
View user's profile Post To page top
syam_talluri Posted: Wed Jan 19, 2005 12:22 pm


Joined: 26 Jan 2005

Posts: 3
[ Doubt ] Bitfields
hi,

the structure is aligned to the boundary of the
types used in the structure

int type boundary
i.e 4

--- "R.Rajesh" <contactrrajesh@...> wrote:

> Hi All,
>
> struct st
> {
> int i : 4;
> int j : 4;
> int k : 4;
> }st;
>
> main()
> {
> printf( " size = %d ", sizeof(st));
> }
>
> The output of the program is 4 . The size of
> structure is sum of size of its member. Hence
> i,j,k are each 4 bits and totally 12 bits(nearly 2
> bytes). But the output is 4 bytes. How this is
> possible ?? I am using Microsoft C++ compiler
> (integer size is 4 bytes)
>
> Thanks in Advance,
> R.Rajesh.
>


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
maniksinghal Posted: Wed Jan 19, 2005 6:32 pm


Joined: 02 Jan 2005

Posts: 9
[ Doubt ] Bitfields
--- "R.Rajesh" <contactrrajesh@...> wrote:

> Hi All,
>
> struct st
> {
> int i : 4;
> int j : 4;
> int k : 4;
> }st;
>
> main()
> {
> printf( " size = %d ", sizeof(st));
> }
>
> The output of the program is 4 . The size of
> structure is sum of size of its member. Hence
> i,j,k are each 4 bits and totally 12 bits(nearly 2
> bytes). But the output is 4 bytes. How this is
> possible ?? I am using Microsoft C++ compiler
> (integer size is 4 bytes)
>
> Thanks in Advance,
> R.Rajesh.
>

Hi Rajesh,
The size of an int is 4 bytes which makes it 32 bits.
What you require in the structure is 12 bits which can
be easily accomodated in 32 bits.
Thats why size is 4.

Regards
Manik




__________________________________
Do you Yahoo!?
The all-new My Yahoo! - Get yours free!
http://my.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
shabble Posted: Wed Jan 19, 2005 11:24 pm


Joined: 29 Dec 2004

Posts: 31
[ Doubt ] Bitfields
From: R.Rajesh [mailto:contactrrajesh@...]

>Hi All,
>
> struct st
> {
> int i : 4;
> int j : 4;
> int k : 4;
> }st;
>
>main()
>{
> printf( " size = %d ", sizeof(st));
>}
>
>The output of the program is 4 The size of structure is sum
>of size of its member. Hence i,j,k are each 4 bits and
>totally 12 bits(nearly 2 bytes). But the output is 4 bytes.
>How this is possible ?? I am using Microsoft C++ compiler
>(integer size is 4 bytes)

It`s called padding. Each instance of `struct st` should start on
(apparently in this case) a 4 byte boundary. If you have an array of
`struct st`, then a padding of 2 bytes and 4 bits would provide this.

`Padding` bits/bytes are inaccessable, and cannot (reliably) be used.

There are compiler options in MSVC which will allow you to change the
default padding. Look in the help files. At a guess, you wil be able to
get sizeof(struct st) down to either 2 or 3 bytes (unsure without
looking whether each member must start on a byte boundary) with the
correct options.

--
PJH

bug, n: An elusive creature living in a program that makes it incorrect.
The activity of "debugging", or removing bugs from a program, ends when
people get tired of doing it, not when the bugs are removed. -
"Datamation", January 15, 1984




Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com Sales : sales@... Service :
service@...

This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...

This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Reply with quote
Send private message
View user's profile Post To page top
alexpally Posted: Thu Jan 20, 2005 12:03 pm


Joined: 21 Jan 2005

Posts: 2
[ Doubt ] Bitfields
There is a concept called padding or byte alignment.
This has been done by most of the compilers for easy &
fast accessing. In ur query, the size of actual
structure is 12 bits(not 12bytes). 12 bits has been
aligned to nearest minimum size of 4bytes

RGDS
Alex Pally


________________________________________________________________________
Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help