Declare a 1 bit variable in C++??

5,575 views
Skip to first unread message

Guna Prasaad

unread,
Jul 24, 2012, 11:54:42 PM7/24/12
to wncc...@googlegroups.com
How do i  a 1 bit variable in C++ ? I firmly think that its possible. 

Guna Prasaad

unread,
Jul 25, 2012, 12:05:49 AM7/25/12
to wncc...@googlegroups.com
I even tried the bit fields in C++. it gives me the same output as earlier : 

program :
struct a {
unsigned x : 3;
};
int main() {
cout<<"Size of boolean type"<<sizeof(a)<<endl; 
return 0;
}

output :
Size of boolean type4

Pritam Baral

unread,
Jul 25, 2012, 12:14:41 AM7/25/12
to wncc...@googlegroups.com
I know memcopy, mmap, and memset aren't strictly C++, but for practical purposes, I think they can be used to achieve what you require.

Regards,

Chhatoi Pritam Baral




On Wed, Jul 25, 2012 at 9:24 AM, Guna Prasaad <guna...@gmail.com> wrote:
How do i  a 1 bit variable in C++ ? I firmly think that its possible. 

--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

Guna Prasaad

unread,
Jul 25, 2012, 12:33:35 AM7/25/12
to wncc...@googlegroups.com
I though about them too. the thing is i am not dealing with destination starting with the start or end of a byte. for eg inserting  2 bits of data from the third bit of the second byte if of an integer type. In this i can dereference the integer type to be char type and reach till the start of second byte but from there i need to reach the 3rd bit.. 
--
Guna Prasaad
Computer Science & Engg Dept, 
IIT Bombay.

Susmit Saraswat

unread,
Jul 25, 2012, 1:20:21 AM7/25/12
to wncc...@googlegroups.com
Two things:

1. The behaviour you're seeing is an indication of alignment at play.
The CPU finds it cumbersome to deal with such tiny transactions in and
out of the memory, so it pads such bitfields. This is standard
behaviour.

2. If you want to truly avail bit-sized variables, do something like
declaring 32 single bit fields in a struct. Short of that, I don't
suppose there's a way of natively overcoming alignment restrictions to
let you operate at bit level.

Guna Prasaad

unread,
Jul 25, 2012, 1:29:18 AM7/25/12
to wncc...@googlegroups.com
I tried using bitset but even that dint work as for all size less than 32, the default is 4 bytes and it acts as if it is 1 bit. 

But then i finally settled down on using struct only. 

Aakash N S

unread,
Jul 25, 2012, 4:22:06 PM7/25/12
to wncc...@googlegroups.com
Have you considered using bit-masks? See :  http://en.allexperts.com/q/C-1040/Bit-Operators.htm, or just Google 'bitmasks C++'

You can use an 'int' to store, access and modify 32 bits of data.

Example :

int x = 0;                   // initialize all bits as 0 
x = x | (1<<10);         // this sets the 10th bit (counting from the LSB, with a 0-based index) to 1.
int a = x & (1<<22);   // this gets the value of the 22nd bit
x = x ^ (1<<12);        // this flips the 12th bit

Is this what you wanted to do?
--
Regards,

Aakash

Anay Joshi

unread,
Jul 26, 2012, 4:28:57 AM7/26/12
to wncc...@googlegroups.com
'bool' is a keyword in C++
"bool x" declares 'x' as a one bit variable.
The actual memory occupied by the bool variable would depend on the implementation of the compiler and the processor involved

RISHABH SINGHAL

unread,
Jul 26, 2012, 1:26:58 PM7/26/12
to wncc...@googlegroups.com
vector<bool> will solve your problem if you are creating a big array.
http://cplusplus.com/reference/stl/vector/

Guna Prasaad

unread,
Jul 27, 2012, 2:35:15 AM7/27/12
to wncc...@googlegroups.com
Thank you. 
@Akash : I wanted to do something similar but I have already implemented it using structures.
@rishabh : yes vector<bool> could have been a good choice. But unless the size of vector is greater than 32 bytes it occupies default 32 bytes only.
Reply all
Reply to author
Forward
0 new messages