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

Memory "plot" and bit packing question

Hello all,

I have a delima. I have 8 5bit numbers that I need to pack into one (or
more) variables. They then must be written (exactly 40bits (5Bytes)) to a
file and have the ability to be put back into memory and then get the 5bit
numbers out again. As I am not by any means proficient in C++ (still
learning) I don't know how to go about doing this correctly. The way I was
planning to do it is as follows:

1. create a character array of 5 chars.
2. create a long long pointer to the array.
3. using shift operators, pack my numbers into the array.
4. write the character data to file. this will lop off any extra
data from the long long
5. read the data back into a different character array.
6. move the pointer to the new array.
7. use bitwise operators to transfer five bits to new int
8. shift the pointer and do 7 again until done.

.... Please don't laugh ... :-)

When 4 is done I read the file with a hex editor and all it is garble. Its
not the bits I'm expecting...

Anyway, if you know of any better way of doing this please point me in the
right direction. I don't want the answer though. I want to learn not just
cut and paste, and any help would be greatly appreciated!

Thanks
Taylor Howell
Jul 23 '05 #1
5 4100
Taylor Howell wrote:
Hello all,

I have a delima. I have 8 5bit numbers that I need to pack into one (or
more) variables. They then must be written (exactly 40bits (5Bytes)) to a
file and have the ability to be put back into memory and then get the 5bit
numbers out again. As I am not by any means proficient in C++ (still
learning) I don't know how to go about doing this correctly. The way I was
planning to do it is as follows:

1. create a character array of 5 chars.
2. create a long long pointer to the array.
3. using shift operators, pack my numbers into the array.
4. write the character data to file. this will lop off any extra
data from the long long
5. read the data back into a different character array.
6. move the pointer to the new array.
7. use bitwise operators to transfer five bits to new int
8. shift the pointer and do 7 again until done.

... Please don't laugh ... :-)

When 4 is done I read the file with a hex editor and all it is garble. Its
not the bits I'm expecting...

Anyway, if you know of any better way of doing this please point me in the
right direction. I don't want the answer though. I want to learn not just
cut and paste, and any help would be greatly appreciated!

Thanks
Taylor Howell

Are you trying to interpret the array of characters as a long? In
general this is a bad idea since you don't know how the bytes of a long
are ordered (look up big-endian and little-endian). Is the ones bit
part of the first byte or the last byte? It's platform dependent in
general.

If you're making an array of chars you should be dealing with chars
only. This may mean a little more work in the case that a 5 bit number
spans the boundary between two chars, but with bit manipulation this is
manageable.

Or you could use std::bitset if that's permissible.
Jul 23 '05 #2
Taylor Howell wrote:
I have a delima. I have 8 5bit numbers that I need to pack into one (or
more) variables. They then must be written (exactly 40bits (5Bytes)) to a
file and have the ability to be put back into memory and then get the 5bit
numbers out again. As I am not by any means proficient in C++ (still
learning) I don't know how to go about doing this correctly. The way I was
planning to do it is as follows:

1. create a character array of 5 chars.
2. create a long long pointer to the array.
There is no such thing as "a long long pointer to the array". In C there
is, but we're not in C.
3. using shift operators, pack my numbers into the array.
4. write the character data to file. this will lop off any extra
data from the long long
5. read the data back into a different character array.
6. move the pointer to the new array.
7. use bitwise operators to transfer five bits to new int
8. shift the pointer and do 7 again until done.

... Please don't laugh ... :-)

When 4 is done I read the file with a hex editor and all it is garble. Its
not the bits I'm expecting...

Anyway, if you know of any better way of doing this please point me in the
right direction. I don't want the answer though. I want to learn not just
cut and paste, and any help would be greatly appreciated!


I would try using 'std::bitset' or 'std::vector<bool>'.

V
Jul 23 '05 #3
hmm, I don't really think there's a 'good' way to do this. Working
with bits is a pretty annoying task in C/C++. About the only thing I
have that could help is a few directions to point ya:
* vector<bool> (or a bit_vector) could be used for the container, but
there's no easy way to output them to a file (assuming one bit per
entry);
* In order to access each independent bit of a character, you can use
this technique:
-------------------------------
struct easy_bit_char {
union {
char c;
struct {
int b0 : 1; int b1 : 1; int b2 : 1; int b3 : 1;
int b4 : 1; int b5 : 1; int b6 : 1; int b7 : 1;
} a;
};
};
--------------------------------
Now you can access each bit of the c member of easy_bit_char using a.b0
through a.b7. For example:
--------------------------------
struct easy_bit_char v;
v.a.b3 = 1;
//Now v.c == 16
v.a.b2 = 1;
//Now v.c == 48
--------------------------------
Of course, writing a function to do all of the shifting and stuff for
you makes this kind of unnecessary, but it's one way to do it.

You could also, in the struct, use this type: long long c:40;
Then define b to contain 40 of the :1 ints. Then you can access each
bit of the 40-bit long long. You should probably also divide it into
characters so that you can output it easily. Oh yea, and make
everything unsigned (working with signs can hold some pitfalls when
working on the bit level).

Er, you say you're still learning C++, so here's a quick explanation:
union { }; - This indicates that every variable declared within the
union takes the *same* place in memory. This was used a lot in C, and
is used more in operating systems programming. We're using that so
that you can access each bit of the variables easily.

int a:1; - in a structure, following the variable name with a :#
indicates how many bits should be allocated to that variable. The rest
of the bits are assumed to be zero, and I think this concept may only
work with integral data types. It's *rarely* used, and is also
something that was carried over from C.

Anyway, good luck. You could probably find a library for bit
manipulation, and writing to files, but it's rare to need, so it might
not be too easy to find.

Jul 23 '05 #4
Err, as the other people who posted said, use bitset. It'd be much
more platform-independent, and better supported than using all of the
union and :1 stuff. Plus more legible. I missed that class on my
look-over of the STL standard.

Jul 23 '05 #5
I think the way you were going is spot on, but here are a few tips:

1. Use an array of 8 characters not 5, otherwise manipulating the
"long long" (which I assume is a 64-bit integer for your compiler)
will modify extra bytes past the end of the array which is not good.
2. Always used unsigned types - try "unsigned long long". This may be
why you are not getting what you expect.
3. Make sure you know the byte order for integers on your system -
whether they are big- or little-endian - and write the right 5 bytes
of the array. Many UNIX machines are big-endian in which case you
need to save bytes 3 to 7.
4. If the code *and* the file format need to be portable then you can
test the byte order and react accordingly. Eg for little-endian write
array elts 0-4 but for big-endian write arrays elts 3-7 in the reverse
order.

A simpler possiblity (but even less portable) is to use bitfields.
However, this relies on your compiler either using a bit-field storage
unit of 64-bit integers (or being able to set it to that) or having a
compiler where bit-fields "straddle" storage units. You will still
need to know the byte order of integers but also need to know the
order that bit-fields are generated by your compiler (top to bottom or
bottom to top).

struct a
{
unsigned int b0: 5;
unsigned int b1: 5;
unsigned int b2: 5;
unsigned int b3: 5;
unsigned int b4: 5;
unsigned int b5: 5;
unsigned int b6: 5;
unsigned int b7: 5;
};

Using this makes it very easy to assign the values. However, most
compilers use a bit-field storage unit of 32-bits (or even 16 bits).
So this will put b0-b5 (30 bits) in the first four bytes (with 2
unused bits), then b6 and b7 in the next storage unit.

Re bitset:
I have never used std::bitset or std::vector<bool> but they are really
designed for manipulating individual bits not 5-bit "mini-integers"
and I also believe they are really intended for use when the numbers
of bits is larger than the biggest supported integer size (apparently
64 bits in your case).

Also using bitset is less portable. Doing direct bit manipulation
means the code is portable to C.
Jul 23 '05 #6

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

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
2
by: George Marsaglia | last post by:
I have a set of, say, 2000 points in the 8-dimensional simplex S={(x_1,x_2,...,x_8),x_1+x_2+...+x_8=1, x's>=0}. To help analyze that 8-dimensional set, I wish to project the set of points onto...
19
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
3
by: Keith Rebello | last post by:
I have an object called ColumnSection which represents a reinforced concrete column. This object contains an arraylist called Diagrams which contains Graph2D objects which are essentially X-Y...
4
by: pcnerd | last post by:
I've been playing with "classic" VB since version 3. I have VB6 Learning Edition. Recently, I wanted to try VB.NET. I got a beginner's book with a CD with the software & installed it. There are...
0
by: cfriedalek | last post by:
I'm writing a python script using win32com to call a 3rd party program via its VBS based API. In VBS a query to get some plot data goes like this: Plot.QueryBegin datacode, Nothing What is...
2
by: Rodrigo Lopez-Negrete | last post by:
Hi all, I'm trying to write a python script using plotting form pylab. Unfortunatelly I've encountered a problem. When I run the script via 'python myscript.py' the plot windows open and close...
0
by: moti far | last post by:
hi, i use stylesheet to design the plot of the print page. The page works with https(ssl) protocol. this my code: <html> <head>
0
by: Just_a_fan | last post by:
Some folks have searched, from time to time, for a dual axis MSChart with different scales on the two Y axes. The sample, extracted from running code I wrote, produces a graph with MSChart (VB9)...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.