473,386 Members | 1,790 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,386 software developers and data experts.

Bit field efficiency

Hello,

I have a class Position which declares a bit field
class Position{
public:
Position(int i=0, int j=0, int k=0);
// Positions have to be within the range: [0..8191,0..8191,0..7]
void setPosition(int i, int j, int k);
int getI();
int getJ();
int getK();
friend ostream& operator<< (ostream& outs, const Position &rhs);
private:
unsigned short i:13;
unsigned short j:13;
unsigned short k:3;
};

How efficient is this technique to save memory (I have a lot of Position
objects)? What size of memory will the three vars i,j,k really occupy in
the memory?

Does this depend on the compiler (icc 7.1 on linux)? On the architecture
(i686)?

Thanks for answers
Phil
Jun 27 '07 #1
7 2191
Philipp wrote:
I have a class Position which declares a bit field
class Position{
public:
Position(int i=0, int j=0, int k=0);
// Positions have to be within the range:
[0..8191,0..8191,0..7] void setPosition(int i, int j, int k);
int getI();
int getJ();
int getK();
friend ostream& operator<< (ostream& outs, const Position
&rhs); private:
unsigned short i:13;
unsigned short j:13;
unsigned short k:3;
};

How efficient is this technique to save memory (I have a lot of
Position objects)?
Compared to what?
What size of memory will the three vars i,j,k
really occupy in the memory?
sizeof(Position)
Does this depend on the compiler (icc 7.1 on linux)? On the
architecture (i686)?
No doubt.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #2
Michael DOUBEZ wrote:
Philipp a écrit :
>[..]

You have to try on your architecture with your compiler. If you are
on a 32bits architecture I would say you should reasonably be able to
pack everything in 32bits.
I wish I could pack *everything* in 32 bits :-)))
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #3
Victor Bazarov a écrit :
Philipp wrote:
> unsigned short i:13;
unsigned short j:13;
unsigned short k:3;
};

How efficient is this technique to save memory (I have a lot of
Position objects)?

Compared to what?
Using complete unsigned shorts.
>
>What size of memory will the three vars i,j,k
really occupy in the memory?

sizeof(Position)
Hmm, yes, definitely.

Phil
Jun 27 '07 #4
Philipp a écrit :
Victor Bazarov a écrit :
>Philipp wrote:
>>What size of memory will the three vars i,j,k
really occupy in the memory?

sizeof(Position)
Ok I tried that...

_Type_ _Size_
bitfield: 4
aligned bitfield: 4
shorts: 6
ints: 12

Phil
Jun 27 '07 #5
Philipp wrote:
Victor Bazarov a écrit :
>Philipp wrote:
>> unsigned short i:13;
unsigned short j:13;
unsigned short k:3;
};

How efficient is this technique to save memory (I have a lot of
Position objects)?

Compared to what?

Using complete unsigned shorts.
And what's stopping you from measuring that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #6
On Jun 27, 1:40 pm, Philipp <sicsic...@freesurf.chwrote:
I have a class Position which declares a bit field
class Position{
public:
Position(int i=0, int j=0, int k=0);
// Positions have to be within the range: [0..8191,0..8191,0..7]
void setPosition(int i, int j, int k);
int getI();
int getJ();
int getK();
friend ostream& operator<< (ostream& outs, const Position &rhs);
private:
unsigned short i:13;
unsigned short j:13;
unsigned short k:3;
};
How efficient is this technique to save memory (I have a lot of Position
objects)? What size of memory will the three vars i,j,k really occupy in
the memory?
It depends somewhat on the implementation, but at the very
least, the last two should pack into a single 16 element; if the
size of an int is more than 29 bits, they should all pack into a
single int. On a machine with 8 bit bytes and byte addressing,
I would expect 4 bytes for the lot; without the bit fields, 6,
or possibly even 8 bytes. So you gain some memory; where bit
fields really start gaining memory, of course, is when you have
a lot of one or two bit fields.

You don't get anything for free, however. Accessing bit-fields
is considerably slower than accessing ints. In some cases, this
can be offset by improved locality; you get more elements in
each page and/or cache line. Most of the time, however,
bit-fields have a very negative impact on run-time. (Note that
writing to a bit-field will normally require two accesses, a
read and a write.)
Does this depend on the compiler (icc 7.1 on linux)? On the architecture
(i686)?
Somewhat. In practice, there's a pretty widespread concensus as
to what is "best practice" for "conventional" architectures, and
the comments above will almost certainly be valid for most, if
not all, mainstream compilers and machines today, and even for
most exotics. The exact layout of the bit-fields will vary,
however.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 28 '07 #7
If you want to be sure it's packed as much as possible, why not set
the bits directly yourself?

Jun 28 '07 #8

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

Similar topics

4
by: Michael Flanagan | last post by:
(Bottom line: I think what I'm looking for is an easy way of changing the case of key values in an array.) I've got code that I'm trying to make agnostic about the underlying database system I'm...
8
by: MS | last post by:
Hello, I have a form with a LoanRequired field which accepts numeric data only. If user enters >14900 then I want the user to continue as normal with the default form. If user enters <1000 then...
2
by: Eddy Stone | last post by:
I would like to be able to use a text input field on my homepage as a way of navigating to other pages within my website... For example, if I typed "diary" into the text field (and pressed...
5
by: Mads Petersen | last post by:
Hi, and thanks for previous help. I use following code to export from excel to access. It is executed in excel. I have an excel spreadsheet with one sheet pr. week. this code is therefore...
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
9
by: RMC | last post by:
Hello, I'm looking for a way to parse/format a memo field within a report. The Access 2000 database (application) has an equipment table that holds a memo field. Within the report, the memo...
6
by: Richard | last post by:
Which way would you guys recommened to best parse a multiline file which contains two fields seperated by a tab. In this case its the linux/proc/filesystems file a sample of which I have included...
1
by: =?Utf-8?B?c2NvdHRybQ==?= | last post by:
I have some code to read the contents of a varbinary(max) field in SQL Server 2005. It is working ok I just wanted to know if anyone had any comments on it's efficiency. Code to read from the...
1
by: news.microsoft.com | last post by:
I have a form field that accepts a start time and one that accepts an end time. I am using a masked editor that allows the user to enter the hour and minutes and am and pm. When I save the form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.