Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,002 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,376 people online right now. Registration is fast and FREE... Join Now!




Booleans in C

2 Pages V  1 2 >  
Reply to this topicStart new topic

Booleans in C

devesa
11 Jan, 2008 - 01:34 PM
Post #1

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
I have to use a booleans array, but it seems that they do not exist in C, so I have done a
CODE
typedef bool char;
.
This way, it works but I'm using too many memory (8 times more). What can I do?

Thanks!
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Booleans In C
11 Jan, 2008 - 01:37 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,457



Thanked: 10 times
Dream Kudos: 325
My Contributions
Use ints. 0 = false, anything else = true.

If you really must, you can include <stdbool.h>.

You could also do this.
CODE

#define TRUE 1
#define FALSE 0

User is offlineProfile CardPM
+Quote Post

devesa
RE: Booleans In C
11 Jan, 2008 - 03:12 PM
Post #3

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
Thanks for your reply, Tom.
I think the problem is the same using ints. Imagine I want to store in an array eight bits (11110000).
Defining bools like ints or chars, this array would take 8 bytes in memory, instead of just 1.

Any other possibility? I cannot find stdbool.h on my Visual Studio.

devesa
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Booleans In C
11 Jan, 2008 - 05:01 PM
Post #4

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,457



Thanked: 10 times
Dream Kudos: 325
My Contributions
This is all that's in it.
CODE

/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

/* As a special exception, if you include this header file into source
   files compiled by GCC, this header file does not by itself cause
   the resulting executable to be covered by the GNU General Public
   License.  This exception does not however invalidate any other
   reasons why the executable file might be covered by the GNU General
   Public License.  */

/*
* ISO C Standard:  7.16  Boolean type and values  <stdbool.h>
*/

#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

#define bool    _Bool
#define true    1
#define false   0

#else /* __cplusplus */

/* Supporting <stdbool.h> in C++ is a GCC extension.  */
#define _Bool   bool
#define bool    bool
#define false   false
#define true    true

#endif /* __cplusplus */

/* Signal that all the definitions are present.  */
#define __bool_true_false_are_defined   1

#endif  /* stdbool.h */


I don't know that much about bits and bytes, but an int is 4 bits on most machines I think? So an array of 8 ints would only be 4 bytes. That's not that bad.

A short int is only 2 bits.

I can't think of any other suggestions. Why are you so worried about memory?
User is offlineProfile CardPM
+Quote Post

Bench
RE: Booleans In C
11 Jan, 2008 - 05:10 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 615



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Tom9729 @ 12 Jan, 2008 - 01:01 AM) *

I don't know that much about bits and bytes, but an int is 4 bits on most machines I think? So an array of 8 ints would only be 4 bytes. That's not that bad.

A short int is only 2 bits.

No, all types must be at least the size of char, according to the standard - and char must be exactly one byte. Of course, there's nothing which specifies how many bits should be in a byte (This is system architecture dependent, although 8 bits is one common size, particularly on personal computers).

C/C++ cannot represent objects smaller than 1 byte - This also remains true when using a struct or union to define bitfields, any type which does not fit into a full byte is subsequently padded to a round number of bytes.
User is online!Profile CardPM
+Quote Post

devesa
RE: Booleans In C
12 Jan, 2008 - 01:09 AM
Post #6

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
Thanks both of you!

stdbool.h I'm afraid only works for C++, but not for C.
Memory is critical in my project because I store every BIT of a file in an array... so I'm using 8 times the size I need, and I really have to solve it.

So there is not another solution than using a BYTE to represent each BIT in the array?

devesa
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Booleans In C
12 Jan, 2008 - 01:23 AM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
When you read data into an array in C, you're reading in a series of bytes. So unless you want to talk compression algorithms, if you read in every byte from the file, your array will be (at minimum) the same size as your file.

How are you reading the data into the file (code please), and how is the data in the file encoded?
User is offlineProfile CardPM
+Quote Post

devesa
RE: Booleans In C
12 Jan, 2008 - 01:31 AM
Post #8

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
QUOTE(jjhaag @ 12 Jan, 2008 - 02:23 AM) *

When you read data into an array in C, you're reading in a series of bytes. So unless you want to talk compression algorithms, if you read in every byte from the file, your array will be (at minimum) the same size as your file.


My array is not the same size as my file, is 8 TIMES this size. Because what I'm doing is:

CODE

charArray=readAllBytes(file); // Defined as char [sizeOfFileInBytes];
boolArray=toBits(charArray); // Defined as char [sizeOfFIleInBytes * 8];
/* toBits gets, for each char, 8 chars representing each bit of the charArray[i] */


What I need is to store in boolArray each BIT (not BYTE) of the file, and to use as less memory as possible.

Thanks!
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Booleans In C
12 Jan, 2008 - 02:02 AM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Well, that doesn't tell us anything - those aren't builtins, so we have no idea what those functions do. But there doesn't appear to be any need to size your array with sizeOfFIleInBytes * 8 - allocating space to hold just sizeOfFIleInBytes bytes should suffoce.


When you read in from a file using, say, fread() or fget(), you're reading in a specified number of bytes from the file. So a file 1024 bytes in size can be read into an character array 1024 elements long.

Now, if your file "example.txt" (for example) is a text file made up entirely of the literal character sequence "0000010100111001", the following code would read the entire file into an array:
CODE
#include <stdio.h>

int main() {
    FILE* myfile;
    myfile=fopen("example.txt","r+");
    
    char array[16];
    
    fread(array, 1, 16, myfile);    //    reads 16objects, each 1 byte long, into <array>
    
    return 0;
}


The file is 16 bytes in size, and the array is 16 bytes in size. If you check the array by sticking in a display loop
CODE

    for (int i=0; i<16; ++i) {
        printf("%c", array[i]);
    }

after the reading step, you'll find the exact same "0000010100111001" displayed.

Now because you only have two types of characters in the file, the literals '0' and '1', there is the possibility that this could be compressed down to 2 bytes (on a system where each byte is made up of 8 bits). But if you're reading the file into an array that is 128 bytes long, you're probably doing something incorrectly. If you provide the actual code that you're using to read in the data (rather than the call to the function where the reading takes place), and an example line or two from the file, we may be able to help some more.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Booleans In C
12 Jan, 2008 - 04:51 AM
Post #10

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 615



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Why not overlay 'bit array' functionality onto your byte array?

Since you know the number of bits in a byte is equal to CHAR_BIT, and you presumably know the size of your byte array, you could do something along these lines -

CODE
unsigned char find_bit_at(unsigned char* arr, int pos)
{
    /*Find the array position of the byte containing the bit */
    int byte_pos = pos / CHAR_BIT;

    /*Find the bit position in the byte */
    int bit_pos = pos % CHAR_BIT;

    return arr[byte_pos] & ( 1<<bit_pos );
}


For example, Assuming CHAR_BIT == 8, if you wanted bit 20 in a 4-byte array (a 32 bit array), the above function would calculate the byte at position 2, of which, it would return the bit at position 4.

(Code is untested, but I hope you see the general idea)
User is online!Profile CardPM
+Quote Post

devesa
RE: Booleans In C
12 Jan, 2008 - 07:15 AM
Post #11

New D.I.C Head
*

Joined: 16 Nov, 2007
Posts: 19


My Contributions
That's a great idea. Thanks all of you for your help.

Best regards,

devesa
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Booleans In C
12 Jan, 2008 - 11:24 AM
Post #12

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,457



Thanked: 10 times
Dream Kudos: 325
My Contributions
QUOTE(devesa @ 12 Jan, 2008 - 02:09 AM) *

Thanks both of you!

stdbool.h I'm afraid only works for C++, but not for C.
Memory is critical in my project because I store every BIT of a file in an array... so I'm using 8 times the size I need, and I really have to solve it.

So there is not another solution than using a BYTE to represent each BIT in the array?

devesa

stdbool.h is part of the C99 standard. It works fine in C. smile.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 12:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month