Discuss bit vector in c in the C Programming forum on Dev Shed. bit vector in c C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
bit vector in c
im trying for sometime to find a way to convert an array of bytes to a long string of bits so i can munipulate them -- (im trying to encode them ) i saw i 1 place that i have to do an array of bollean is that right ? and if so im looking for a code that do that (im not so strong in programing as i in hardware .... ( e.e ) )
Posts: 1,713
Time spent in forums: 1 Month 2 Weeks 1 Day 33 m 13 sec
Reputation Power: 1495
You don't need to convert the data at all; using the appropriate shifts, bitwise operators and bit masks, you can manipulate the values directly. You'll probably want functions to isolate and modify a specfic bit in a given byte.
[code removed, see below]
Not tested code; use at your own discretion. You should be able to use this allong with the appropriate manipulation of the array to get the desired effect. HTH.
EDIT: After I wrote this, I remembered a set of functions to do exactly what you want which I had found in Programing Pearls and had used in some memory-mapping code. Here it is:
Code:
// bitmap.h - header file for basic bitmap manipulation types and functions
#ifndef ___BITMAP_H___
#define ___BITMAP_H___
typedef enum {CLEAR = 0, SET = 1} BIT;
typedef unsigned int BITMAP;
#define MAXBITS 32
BIT testbit(BITMAP* map, int index);
void setbit(BITMAP* map, int index);
void clearbit(BITMAP* map, int index);
#endif
Code:
// bitmap.c - basic bitmap functions
// based on code from _Programming Pearls_, 2nd Ed. by Jon Bentley
#include "bitmap.h"
#define SHIFT 5
#define MASK 0x1F
BIT testbit(BITMAP* map, int index)
{
return (BIT) (map[index >> SHIFT] & (1 << (index & MASK)));
}
void setbit(BITMAP* map, int index)
{
map[index >> SHIFT] |= (1 << (index & MASK));
}
void clearbit(BITMAP* map, int index)
{
map[index >> SHIFT] &= ~(1 << (index & MASK));
}
FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
Last edited by Schol-R-LEA : December 18th, 2004 at 05:31 PM.
Posts: 261
Time spent in forums: 1 Day 5 h 33 m 40 sec
Reputation Power: 8
An array of bool will probably not be what you want. You have to remember that on a computer, a bit is not a directly addressable unit of storage; a byte is. If something can be a variable, it has an address (unless it's a register variable, but then the size is generally the native word size of the processor), hence it's addressable, and thus it follows it's at least one byte in size. To get at a single bit, you need to do bitwise math on a larger type.
In C/C++, you will generally use an integer of some size; this depends on exactly what you're doing. Larger types mean more bits per unit of storage. Also remember that this sort of thing is rather non-portable unless you make special efforts; the sizes of various data types differ from compiler to compiler. For example, a short int on one system might be 16 bits, but on another, it may be 32. Even a byte might not be 8 bits; the definition of a byte is simply 'the smallest addressable unit of memory of a computer'. You can check <limits.h> for the ranges that each type can accommodate, and create typedefs as appropriate; I think the latest C standard (C99) does something similar for you. Also, use unsigned types; this will eliminate any chance that the sign bit will cause you problems.
Another thing to be careful of is the byte order. Intel x86 CPUs are little-endian, whereas Motorola CPUs are big-endian. This means the value 0xAABBCCDD will be stored in memory as 0xDD, 0xCC, 0xBB, and finally 0xAA on a little-endian, but on a big-endian machine, it will be the 'expected' 0xAA, 0xBB, 0xCC, and 0xDD. For this reason, it might be safest to use char values; these generally correspond to a single machine byte, and thus, byte order will be unaffected.
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for all -- but
first thank u gave me some good things to think of ....
well im trying to write a program that do a convalotional code = that code is a member of the error correcting code famliy
im dealing with a bmp file (24 bit) that i want to encode and then send = then check the error (BER - bit error rate & some more errors...) i encode bits by doing XOR in a buffer = i have the buffer and i wrote the encoder to but the encoder work on single bits bit by bit without meaning to the byte they came from;while the buffer is in typedef byte so i need convert the array to of bytes to one long string of bits . i got this: http://www.csd.uwo.ca/%7ejamie/BitVectors/bitarr.c
that is close to what im doing but its a long code
im writing in c code as u can c
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
Posts: 261
Time spent in forums: 1 Day 5 h 33 m 40 sec
Reputation Power: 8
You're trying to ensure the file isn't corrupt after sending it across the net, or transmitting it in some other way? My advice: don't bother. The people who write the error-correction algorithms that are in network transport protocols, modem firmware, and the like have a lot more experience with this sort of thing than you or I. If it gets corrupted despite this, your code won't be doing a better job of detecting this, and if it doesn't get corrupted, your code is superfluous.
I realize you might be doing this as a learning exercise, but if you're anything like me, you learn better when you're doing something that has a point. If you're still set on doing it, well, good luck.
Posts: 229
Time spent in forums: 2 h 13 m 58 sec
Reputation Power: 9
hehe. i've just finished my final exam last week on communication theory (undergraduate EE).
if you are trying to implement 'convolution,' all you have to do is synthetic polynomial multiplication. if you are doing error correction, then you need to create a parity check matrix and compute the syndrome, etc. i think tcp/ip doesn't use error correction. is there a name for the error correction method you are trying to do?
Posts: 229
Time spent in forums: 2 h 13 m 58 sec
Reputation Power: 9
i am having more fun. here is another little prog for sarr. it is only for giving ideas.
Code:
/*
this program demonstrates the workings of a convolutional
encoder with K=3 (2,1)
this is an example taken from "Digital Communications"
ISBN 0-13-091722-2 page 387. There are n = 2 modulo-2 (XOR gate)
adders; thus the code rate is k/n = 1/2.
-----------
| |
top XOR (+) o u1
-----
| | |
INPUT 101--> [0][0][0] o----x(flips up and down)---> OUTPUT
| |
-----
bottom XOR (+) o u2
| |
-----------
*/
int main(){
/*
our intended input is 101, which translates into 5 decimal.
however, the 3 bit register initially contains [0][0][0].
so our input will be 101000 instead of 101 because the last three
bits will be represent our working register.
101000 will be shifted to the right as time advances and we
will be checking the least significant three bits.
we start with 101000 = 8+32 = 40 decimal
*/
unsigned int input = 40; // 101000
// we create three masks to isolate each of the three last bits of our register.
unsigned int m0 = 1; // ...0000001 = 1 decimal
unsigned int m1 = 2; // ...0000010 = 2 decimal
unsigned int m2 = 4; // ...0000100 = 4 decimal
// we declare an array that will store the value of the working register.
unsigned int regi[3]={0,0,0};
// we declare an array to store the output (20 is plenty for this example).
unsigned int output[20] = {0};
// we start the encoder by shifting the input to the right -> 101[0][0][0]
// if we shift 1 bit to the right the register looks like 10 ---->[1][0][0]
input = input >> 1;
// we take a reading into the register
regi[0] = (input & m0)?1:0;
regi[1] = (input & m1)?1:0;
regi[2] = (input & m2)?1:0;
//now we apply the top XOR gate (all three inputs)
output[0] = regi[0] ^ regi[1] ^ regi[2]; // 1 ^ 0 ^ 0 = 1
//we should now apply the bottom XOR gate (from the diagram, bits 0 and 2)
output[1] = regi[0] ^ regi[2];
/*
since the rest looks all the same (repetitive), we do a for loop
so the computer does all the work for us at least 18 more times ;)
*/
int i;
for(i = 2; i < 20; i += 2){
input = input >> 1;
regi[0] = (input & m0)?1:0;
regi[1] = (input & m1)?1:0;
regi[2] = (input & m2)?1:0;
output[i] = regi[0] ^ regi[1] ^ regi[2];
output[i+1] = regi[0] ^ regi[2];
}
// we send the output to the screen
for(i = 0; i <20; i++)
printf("%u ",output[i]);
printf("/n");
return 0;
}
Last edited by arpia : December 20th, 2004 at 07:14 PM.
Posts: 5,319
Time spent in forums: 2 Weeks 2 Days 22 h 4 m 31 sec
Reputation Power: 212
As I remember TCP rides on top of IP which rides on top of some other protocol (Ethernet in most common applications) which does not use error _correction_, but it does use error _detection_. If a packet is corrupt it is just resent, no effort is made to figure out what the problem was.