473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do you declare a data type with 1 bit size?

How can you declare a data type with 1 bit size? So I can have a true boolean and not just an int or enum.
Mar 7 '10 #1

✓ answered by Banfa

You can't. You could create a bit field structure, you can specify the size of the fields in a structure of bits fields in bits. However the compiler will round the type to at the minimum the nearest byte so you save nothing and the fields are not addressable and the order that bits in a bit field are stored is implementation defined.

Generally bit firlds are best avoided, especially for portable code.

If you want to store several boolean values in 1 byte then use the bitwise operators to directly access the bits in the byte, otherwise live with a 1 byte boolean.

3 10715
Banfa
9,065 Expert Mod 8TB
You can't. You could create a bit field structure, you can specify the size of the fields in a structure of bits fields in bits. However the compiler will round the type to at the minimum the nearest byte so you save nothing and the fields are not addressable and the order that bits in a bit field are stored is implementation defined.

Generally bit firlds are best avoided, especially for portable code.

If you want to store several boolean values in 1 byte then use the bitwise operators to directly access the bits in the byte, otherwise live with a 1 byte boolean.
Mar 7 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
Are you using C++?

If so, try a bitset:

Expand|Select|Wrap|Line Numbers
  1. bitset<16> switches;
That'll give you 16 addressable switches:

Expand|Select|Wrap|Line Numbers
  1. data.set(3) = true;  //set the 4th switch
The size of the bitset will be 4 bytes (an int) so you could have 32 switches in a 4 byte field. More than that the bitset object will increase in size by the sizeof an int.
Mar 8 '10 #3
donbock
2,426 Expert 2GB
The bool type in C99 acts like a 1-bit variable, but there is no telling how much storage space it actually takes up.
Mar 8 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
2
by: Steve | last post by:
Hi; I am trying to write a rountine ( below ) that will go into a colum of text data type ( fae.pmcommnt ) locate the word "to" and replace it. I have the routine below. I get no error...
4
by: Robert W. | last post by:
I have a data model that has dozens of properties of most every data type. I now want to declare a few of these properties as Images but can't seem to do it. The approach I thought would work...
1
by: LongBow | last post by:
Hello, I am attempting to create an application that uses an existing driver interface API using C#. I have an API that looks like F32x_Read( HANDLE Handle, LPVOID Buffer, DWORD...
5
by: chellappa | last post by:
Hi All, How to find the data type of the variable ? is there any libaray function avaiable? Because i want create generic data type of some operation . Thanks All By Chellappa
13
by: Fei Liu | last post by:
Hi Group, I've got a problem I couldn't find a good solution. I am working with scientific data files in netCDF format. One of the properties of netCDF data is that the actual type of data is only...
9
by: jc | last post by:
Hi all, I have a data type to use that I can't modify its codes. E.g. This template data type is data_type. When I declare a variable of this data_type, I write as following: data_type(8,...
76
by: KimmoA | last post by:
First of all: I love C and think that it's beautiful. However, there is at least one MAJOR flaw: the lack of a boolean type. OK. Some of you might refer to C99 and its _Bool (what's up with the...
1
by: aviansh | last post by:
HI Experts, I have same table structures in two database and one master table which contains Table id, Table name,primary key, data type of primary key. i have to comapare Tables in both tha...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.