C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming
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.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2004, 02:45 PM
sarr sarr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 sarr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question 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 ) )

thank any way anything will go

Reply With Quote
  #2  
Old December 18th, 2004, 04:23 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,713 Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level) 
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));
}
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

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.

Reply With Quote
  #3  
Old December 18th, 2004, 05:31 PM
lectricpharaoh's Avatar
lectricpharaoh lectricpharaoh is offline
Ultimate Obliterator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wet West Coast of Canada
Posts: 261 lectricpharaoh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old December 18th, 2004, 11:18 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 361
Send a message via AIM to L7Sqr
if you are working with c++ you may want
to look into a vector<bool> or bitset<N>
where N is a number of bits.
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #5  
Old December 19th, 2004, 03:35 AM
sarr sarr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 sarr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question 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

and thank u again !!!!

Reply With Quote
  #6  
Old December 19th, 2004, 08:28 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,319 mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 4 m 31 sec
Reputation Power: 212
FYI, I have some bit manipulation classes here: http://forums.devshed.com/t125318/s.html. I also wrote a version for C.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

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

Reply With Quote
  #7  
Old December 19th, 2004, 10:38 AM
lectricpharaoh's Avatar
lectricpharaoh lectricpharaoh is offline
Ultimate Obliterator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Wet West Coast of Canada
Posts: 261 lectricpharaoh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old December 19th, 2004, 03:20 PM
arpia's Avatar
arpia arpia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Baltimore, MD
Posts: 229 arpia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #9  
Old December 19th, 2004, 06:36 PM
arpia's Avatar
arpia arpia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Baltimore, MD
Posts: 229 arpia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 13 m 58 sec
Reputation Power: 9
just for fun i made this small program that shows how to calculate the convolution of two vectors

Code:
// convolution.c

#include <stdio.h>

int main(){

  // input vectors
  int A[5]={1,2,3,4,5};
  int B[5]={3,2,1,0,1};

  // working matrix 
  int C[20][20]={0};

  //result
  int R[20]={0};

  int k,i;

  //populate the working matrix
  for( k = 0; k < 5; k++)
    for( i = 0; i < 5; i++)
      C[i+k][k] = A[i] * B[k];

  //save in R
  for(k = 0; k < 19; k++)
    for(i = 0; i < 19; i++)
      R[k] += C[k][i];  

/*
   3  6  9   12   15                   B[0]*A[]
      2  4   6    8   10               B[1]*A[]
         1   2    3   4   5            B[2]*A[]
             0    0   0   0  0         B[3]*A[]
                  1   2   3  4   5     B[4]*A[]
   --------------------------------    ++++++++
   3  8  14  20   27  16  8  4   5     R[]
*/

  //send output to screen
  for(i = 0; i <= 5+5-2; i++)
    printf("%i ",R[i]);

  printf("\n");

  return 0;
}


remember to pad with zeros

Reply With Quote
  #10  
Old December 20th, 2004, 07:12 PM
arpia's Avatar
arpia arpia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Baltimore, MD
Posts: 229 arpia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #11  
Old December 20th, 2004, 08:44 PM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God (5000 - 5499 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,319 mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level)mitakeet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > bit vector in c


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap