title: Boolean array using "char"
body: <p>I made an object that actually represents an array of 8 booleans stored in a char. I made it to learn something more about bitwise operators and about creating your own objects in C. So I've got two questions:</p>
  
  <ol>
  <li>Can I be certain if the below code
  always works?</li>
  <li>Is this a good implementation to
  make an object that can't get lost
  in C, unless you release it
  yourself.</li>
  </ol>
  
  <p><strong>The Code:</strong></p>
  
  <pre><code>/*
   *  IEFBooleanArray.h
   *  IEFBooleanArray
   *
   *  Created by ief2 on 8/08/10.
   *  Copyright 2010 ief2. All rights reserved.
   *
   */
  
  #ifndef IEFBOOLEANARRAY_H
  #define IEFBOOLEANARRAY_H
  
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  
  typedef char * IEFBooleanArrayRef;
  
  void IEFBooleanArrayCreate(IEFBooleanArrayRef *ref);
  void IEFBooleanArrayRelease(IEFBooleanArrayRef ref);
  int IEFBooleanArraySetBitAtIndex(IEFBooleanArrayRef ref, 
                                   unsigned index, 
                                   int flag);
  int IEFBooleanArrayGetBitAtIndex(IEFBooleanArrayRef ref, 
                                   unsigned index);
  
  #endif
  </code></pre>
  
  <hr>
  
  <pre><code>/*
   *  IEFBooleanArray.c
   *  IEFBooleanArray
   *
   *  Created by ief2 on 8/08/10.
   *  Copyright 2010 ief2. All rights reserved.
   *
   */
  
  #include "IEFBooleanArray.h"
  
  void IEFBooleanArrayCreate(IEFBooleanArrayRef *ref) {
      IEFBooleanArrayRef newReference;
  
      newReference = malloc(sizeof(char));
      memset(newReference, 0, sizeof(char));
      *ref = newReference;
  }
  
  void IEFBooleanArrayRelease(IEFBooleanArrayRef ref) {
      free(ref);
  }
  
  int IEFBooleanArraySetBitAtIndex(IEFBooleanArrayRef ref, unsigned index, int flag) {
      int orignalStatus;
  
      if(index &lt; 0 || index &gt; 7)
          return -1;
  
      if(flag == 0)
          flag = 0;
      else
          flag = 1;
  
      orignalStatus = IEFBooleanArrayGetBitAtIndex(ref, index);
      if(orignalStatus == 0 &amp;&amp; flag == 1)
          *ref = *ref + (int)pow(2, index);
      else if(orignalStatus == 1 &amp;&amp; flag == 0)
          *ref = *ref - (int)pow(2, index);
  
      return 0;
  }
  
  int IEFBooleanArrayGetBitAtIndex(IEFBooleanArrayRef ref, unsigned index) {
      int result;
      int value;
  
      value = (int)pow(2, index);
      result = value &amp; *ref;
  
      if(result == 0)
          return 0;
      else
          return 1;
  }
  </code></pre>
  
  <p>I'm more of an Objective-C guy, but I really want to learn C more. Can anyone request some more "homework" which I can improve myself with?</p>
  
  <p>Thank you,
  ief2</p>