473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Representing a Sequence of Shorts in Bits

Is there a sample in C out there to represent a sequence of numbers
(shorts) using bits? With the intention of saving space, instead of
using the full 16 bits per short. Thanks.

- Hahnemann
Jun 27 '08 #1
8 2328
Hi

On Wed, 25 Jun 2008 15:23:28 -0700, Hahnemann wrote:
Is there a sample in C out there to represent a sequence of numbers
(shorts) using bits? With the intention of saving space, instead of
using the full 16 bits per short. Thanks.
So, if the short is 0, you set the bit to 0.
If the short is 1 you set the bit to 1.
If the short is 2... do you see the problem yet?
The bit can only contain 0 or 1.

If you have a large number of shorts, and there is significant
correlation between them, ie: many of them have the same value or they
follow some pattern, then you can compress the data and save some space
at the expense of time, but even then in most cases you will do well to
get 16:1.

Try http://google.com/search?q=compression+algorithms

HTH
Jun 27 '08 #2
viza <to******@gmil.comwrites:
On Wed, 25 Jun 2008 15:23:28 -0700, Hahnemann wrote:
>Is there a sample in C out there to represent a sequence of numbers
(shorts) using bits? With the intention of saving space, instead of
using the full 16 bits per short. Thanks.

So, if the short is 0, you set the bit to 0.
If the short is 1 you set the bit to 1.
If the short is 2... do you see the problem yet?
The bit can only contain 0 or 1.
I think the OP is looking represent a set of smallish integers as a
bit set. The term sequence is confusing but the OP had written "set"
everywhere where they wrote "sequence" I think you'd have offered a
different answer.

To the OP: search for "bit set in C" or something like that.

--
Ben.
Jun 27 '08 #3
Hahnemann wrote:
>
Is there a sample in C out there to represent a sequence of numbers
(shorts) using bits? With the intention of saving space, instead of
using the full 16 bits per short. Thanks.
shorts can have any number of bits. To get an idea of the size on
your system, have your machine print out the value of USHRT_MAX,
after "#include <limits.h>". Another indicative value is
"(sizeof(short) * CHAR_BIT)".

You can save unsigned values upto USHRT_MAX in an unsigned short.
Upto UCHAR_MAX in an unsigned char. Remove the Us and you can save
negative values, down to SHRT_MIN and SCHAR_MIN, in short and
signed char. Now you will have to use SHRT_MAX and SCHAR_MAX for
the upper limits.

Read that limits.h file.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #4
On Jun 26, 3:23 am, Hahnemann <hahnemann.or...@gmail.comwrote:
Is there a sample in C out there to represent a sequence of numbers
(shorts) using bits? With the intention of saving space, instead of
using the full 16 bits per short. Thanks.

- Hahnemann
You can save some space by using bitwise operators to store and
retrieve the values but remember, you can not actually read and write
in terms of bits ( not even in assembly/machine language). If you are
writing 1, even though it can be represented using 1 bit, you will
have to use 1 byte.

I am not aware how a typical bit set works but that too is not going
to very helpful in saving space.
Jun 27 '08 #5
On Jun 25, 11:16*pm, rahul <rahulsin...@gmail.comwrote:
I am not aware how a typical bit set works but that too is not going
to very helpful in saving space.
If the platform provides a 16 bit short, and the range of values the
OP is interested only range between, for example, 0-511, then you
could pack each number into 9 bits. This would be nearly half the
storage space.

Obviously, the utility of such a technique depends on the situation.

Jun 27 '08 #6
I read the following from a book called "Compression Algorithms for
Real Programmers":

"...Programmers have long known that it is silly to use double
precision values or long integers when regular floating point numbers
or integers will serve the purpose... David Kopf takes this one step
further with an algorithm that carefully adjusts the number of bits in
a sequence. If this is too many bits, then it reduces the size by i
bits for the next value. If it is too few, then it increases the size
by j bits and repeats the value. In some cases, the algorithm uses a
fixed i and j, and, in others, it uses a preset number".

I am wondering how this works in actual C code. I Googled that
algorithm but I can't find it. Does this make sense? Maybe I'm
getting this "packing of bits" idea incorrectly.

- Hahnemann
Jun 27 '08 #7
Hahnemann wrote:
I read the following from a book called "Compression Algorithms for
Real Programmers":

"...Programmers have long known that it is silly to use double
precision values or long integers when regular floating point numbers
or integers will serve the purpose... David Kopf takes this one step
further with an algorithm that carefully adjusts the number of bits in
a sequence. If this is too many bits, then it reduces the size by i
bits for the next value. If it is too few, then it increases the size
by j bits and repeats the value. In some cases, the algorithm uses a
fixed i and j, and, in others, it uses a preset number".

I am wondering how this works in actual C code. I Googled that
algorithm but I can't find it. Does this make sense? Maybe I'm
getting this "packing of bits" idea incorrectly.
It's wrong in C.

Your default choice of floating point type should be (double).
If you want the smallest, then use (float).
If you want the one with the most precision or range,
then use (long double).

(float) is implicitly converted to (double)
by the default argument promotions,
which indicates that (double)
is the more natural of the two types.

"%f" is the fprintf format specifier for type (double).

--
pete
Jun 27 '08 #8
Hahnemann said:
I read the following from a book called "Compression Algorithms for
Real Programmers":

"...Programmers have long known that it is silly to use double
precision values or long integers when regular floating point numbers
or integers will serve the purpose...
Programmers have long known lots of wrong things. Er... so what?

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9

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

Similar topics

1
2310
by: Hemant Shah | last post by:
Folks, I am having problem running select query from a sequence. I get SQL0204N error. I am running UDB 8.2 on AIX 5.2. # oslevel 5.2.0.0 # db2level DB21085I Instance "dba2test" uses...
3
2492
by: DFS | last post by:
FYI, Using DLookup("ResultsField","Pass-thru query") consumes 2 SEQUENCE numbers each time it's called. Anyone know why?
4
7696
by: Kamran K | last post by:
Hello I have created a client server application using C#. Existing application is using random number on client side to generate sequence numbers that are then assigned to transactions. This...
15
9617
by: Robby Russell | last post by:
I am trying to track down a method of determining what a sequence name is for a SERIAL is in postgresql. For example, CREATE TABLE foo (id SERIAL PRIMARY KEY NOT NULL, bar TEXT); \d foo...
7
3584
by: urban.widmark | last post by:
Hello We are having some problems with triggers, sequences and union all in V8 on code that worked fine in V7. Was wondering if someone else has seen this and/or knows what to do. A trigger...
5
6789
by: cboth | last post by:
Hello everyone! I'm searching for a Random Binary Sequence which creates the outcome of an XOR gate. I need to create a sequence of 2^(7) -1 bits....using my C compiler. Thank you all for your...
4
2066
by: penzer | last post by:
how to exchange 1234(number) -bytes sequence in the C#?
8
5600
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
8
3648
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
0
7353
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
7418
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...
1
7075
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
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
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...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.