Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ pointer type pointing to single bit?

Tags:

c++

c

pointers

I want to modify individual bits of data, (for e.g. ints or chars). I want to do this by making a pointer, say ptr. by assigning it to some int or char, and then after incrementing ptr n times, I want to access the nth bit of that data. Something like

// If i want to change all the 8 bits in a char variable
char c="A";
T *ptr=&c; //T is the data type of pointer I want..
int index=0;
for(index;index<8;index++)
{
    *ptr=1;  //Something like assigning 1 to the bit pointed by ptr...
}
like image 995
tigerden Avatar asked Apr 06 '13 07:04

tigerden


People also ask

Does pointer point to a bit?

So a bit has no address and as such a pointer cant point to it. You could use a char * and bitwise operations to determine the value of individual bits.

Can pointers point to any data type?

Every pointer is of some specific type. There's a special generic pointer type void* that can point to any object type, but you have to convert a void* to some specific pointer type before you can dereference it.

Can a pointer be any type in C?

void pointer in C / C++A void pointer can hold address of any type and can be typecasted to any type.

Do pointers point to address?

Pointers are said to "point to" the variable whose address they store. An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by".


3 Answers

There no such thing as a bit pointer in C++. You need to use two things, a byte pointer and an offset to the bit. That seems to be what you are getting towards in your code. Here's how you do the individual bit operations.

// set a bit
*ptr |= 1 << index;

// clear a bit
*ptr &= ~(1 << index);

// test a bit
if (*ptr & (1 << index))
    ...
like image 161
john Avatar answered Oct 19 '22 18:10

john


The smallest addressable memory unit in C and C++ is 1 byte. So You cannot have a pointer to anything less than a byte.If you want to perform bitwise operations C and C++ provide the bitwise operators for these operations.

like image 1
Alok Save Avatar answered Oct 19 '22 20:10

Alok Save


It is impossible to have address of individual bit, but you can utilize structures with bit fields. Like in this example from Wikipedia so:

struct box_props
{
     unsigned int opaque       : 1;
     unsigned int fill_color   : 3;
     unsigned int              : 4; // fill to 8 bits
     unsigned int show_border  : 1;
     unsigned int border_color : 3;
     unsigned int border_style : 2;
     unsigned int              : 2; // fill to 16 bits
};

Then by manipulating individual fields you will change sets of bits inside unsigned int. Technically this is identical to bitwise operations, but in this case compiler will generate the code (and you have lower chances of bug).

Be advised that you have to be cautious using bit fields.

like image 1
Roman Saveljev Avatar answered Oct 19 '22 20:10

Roman Saveljev