Boolean arrays using "char"

I created an object that actually represents an array of 8 boolean elements stored in char. I did this to learn more about bitwise operators and how to create your own objects in C. Therefore, I have two questions:

  • Can I be sure that the code below always works?
  • This is a good implementation for making an object that cannot be lost in C unless you release it yourself.

The code:

/*
 *  IEFBooleanArray.h
 *  IEFBooleanArray
 *
 *  Created by ief2 on 8/08/10.
 *  Copyright 2010 ief2. All rights reserved.
 *
 */

#ifndef IEFBOOLEANARRAY_H
#define IEFBOOLEANARRAY_H

#include <stdlib.h>
#include <string.h>
#include <math.h>

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

/*
 *  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 < 0 || index > 7)
        return -1;

    if(flag == 0)
        flag = 0;
    else
        flag = 1;

    orignalStatus = IEFBooleanArrayGetBitAtIndex(ref, index);
    if(orignalStatus == 0 && flag == 1)
        *ref = *ref + (int)pow(2, index);
    else if(orignalStatus == 1 && 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 & *ref;

    if(result == 0)
        return 0;
    else
        return 1;
}

I am more of an Objective-C guy, but I really want to learn more C. Can someone ask for more “homework” with which I can improve myself?

Thank ief2

+3
source share
5

pow(2,index) . , , pow() . (1<<index). , C'ish / . :


C, , , "" - :


:

if(result == 0)
        return 0;
    else
        return 1;

return (result != 0);, return result return !!result ( 0 1). , C " "; C . iffy, , : " - Java " - .


newReference = malloc(sizeof(char));
memset(newReference, 0, sizeof(char));

malloc + memset(x,0,z) == calloc();


( ) IEFBooleanArraySetBitAtIndex, IEFBooleanArrayGetBitAtIndex. . .

+4
  • < 0, .
  • (unsigned int, unsigned char ..).
  • flag == 0, 0?
  • * typedef, . [/li >
  • memset(), 0.
  • pow . << >>
  • if .
  • & | + - SetBitAtIndex, if.
  • GetBitAtIndex index.

# 9 - , , , . - .

+10

#n char, pow() shifting masking:

#n:

a = a | (1 << n);

#n:

a = a & (~(1 << n));

#n:

return ((a >> n) & 1);
+3

, , ( ), ... , malloc(sizeof(char))? . . char.

, : typedef char IEFBoolArray; IEFBoolArray. typedef struct { char value; } IEFBoolArray; , , . .

... , , char? , - , int.

+1

Carl Norum:

  • char , (.. ). , ..
  • mallocing char. 4 8 , char , , malloced chunk .
  • , , . .

3- - :

typedef struct {
    uint64_t size;
    uint64_t *array;
}bitarray;

bitarray bitarray_new(uint64_t size) {
    bitarray arr;
    arr.size = size;
    arr.array = calloc(size/8);
    return arr;
}

void bitarray_free(bitarray arr) {
    free(arr.array);
}

void bitarray_set(bitarray arr, uint64_t index, int bit) {
  assert (index <= arr.size)
  if (bit)
    array[index/8] |= 1 << (index % 8);
  else
    array[index/8] ^= ~(1 << (index % 8));
}

void bitarray_get(bitarray arr, uint64_t index, int bit) {
  assert (index <= arr.size)
  return array[index/8] & 1 << (index % 8);
}

Copyright 2010 ief2. All rights reserved.

. cc-by-sa , . , , , .

(PS. I would advise you not to publish a trivial work on restrictive licenses in any case - it does not look professional - if you have no legal problems)

Is this a good implementation to make an object that cannot be lost in C unless you release it yourself.

Unfortunately?

+1
source

Source: https://habr.com/ru/post/1758564/


All Articles