Solved

How to define a binary variable in Turbo c

Posted on 1998-12-16
8
2,313 Views
Last Modified: 2008-02-07
How to define in Turbo C language for a binary variable or an array variable with only 1-bit allocated size for each array element. Can I implement my program in this way ?
0
Comment
Question by:dicksonay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1255200
You can have individual bits only take up one bit using :1
eg.

struct {
  int a : 1;
  int b : 1;
  int c : 1;
  int d : 1;
};

the four vars will only take up 1 bit each (but the whole struct will take up at least a byte because of packing).

However, you cannot do the same with an array.

Instead you'll need code that allows you to allocate and manipulate a block of storage (several bytes) as an array of bits.

I can point you to a site that (should) have such code.


0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255201
Ronslow, you beat me to it, oh well submitting as a comment

C includes a bitfield data type

int x:1 ; /* x is 1 bit */

Unfortunately if you stick these in array the compiler will probably pad these variables out (wasting the space).  

Therefore the normal way to do bit arrays, is to do the bit manipulation yourself.

Create a series of functions :-

(1) malloc an array of chars to allocate the array. The array should be 1/8 the number of bits required (round up if you array size is not exactly divisible by 8)

say
char * pArray = (char *)malloc( 128 / 8 ) ; /* Array size is 128 bits */

(2) set the initial state of the array to 0
memset( pArray, 0, 128/8 ) ;

(3) Set a bit in the array using code :-

say index is an integer to the offset in the array
*(pArray+(index/8)) |= ( 1 << (index % 8 ) ) ;

(4) get a bit in the array using
char byte = *(pArray+(index/8))
int bitshift =  byte & ( 1 << (index % 8 ) ) ; /* 0 if bit not set, non-zero if set */
int bit = bitshift >> ( index % 8 ) ; /* 0 if bit not set, 1 if bit is set */



0
 

Author Comment

by:dicksonay
ID: 1255202
What is the code you are saying ? Can you please clarify it ?
- a turbo C function call or
- an external calling function or ...

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255203
from a very useful site: http://snippets.org/

bitarray.c

/* +++Date last modified: 05-Jul-1997 */

/*
**  Functions to maintain an arbitrary length array of bits
*/

#include "bitops.h"

char *alloc_bit_array(size_t bits)
{
        char *set = calloc((bits + CHAR_BIT - 1) / CHAR_BIT, sizeof(char));
        return set;
}

int getbit(char *set, int number)
{
        set += number / CHAR_BIT;
        return (*set & (1 << (number % CHAR_BIT))) != 0;    /* 0 or 1   */
}

void setbit(char *set, int number, int value)
{
        set += number / CHAR_BIT;
        if (value)
                *set |= 1 << (number % CHAR_BIT);           /* set bit  */
        else    *set &= ~(1 << (number % CHAR_BIT));        /* clear bit*/
}

void flipbit(char *set, int number)
{
        set += number / CHAR_BIT;
        *set ^= 1 << (number % CHAR_BIT);                   /* flip bit */
}

Hope this helps


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255204
sorry for the delay .. I had to lookup where the snippets site was.

this site has (mostly) portable code.

Note that the code above doesn't even assume 8-bits per char variable (a char doesn't need to be 8-bits)

bitops.h can also be found at that site (but all you really need are prototypes for the functions used)

0
 

Author Comment

by:dicksonay
ID: 1255205
Ronslow, your answer is pretty userful for me. I am thanks for your participation.

And, I also thanks to Expert Answers2000 for his comment.

I understand that it may be no alternative method to optimize my program for the purpose of one bit arrary.

Really appreciated
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255206
the question is .. is the storage saving worth the extra time required for accessing individual bits.

Also note that one advantage of the bit-array is that it is possible to AND or OR etc a pair of arrays very quickly.  It is also possible to set/clear all bits very quickly.  That is because there are less actual bytes being used.

eg.

void clearbits(char* set, size_t bits)
{
        int i;
        int n = (bits + CHAR_BIT - 1) / CHAR_BIT;
        for (i = 0; i < n; i++)
                set[i] = 0;
}


void orbits(char* set1, char* set2, size_t bits)
{
        int i;
        int n = (bits + CHAR_BIT - 1) / CHAR_BIT;
        for (i = 0; i < n; i++)
                set1[i] |= set2[i];
}


0
 

Author Comment

by:dicksonay
ID: 1255207
Thank a lot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question