how to create pointer to a bit in c-language
Solution 1
The char is the 'smallest addressable unit' in C. You can't point directly at something smaller than that (such as a bit).
Solution 2
You can't. Using pointers, it's not possible to manipulate bits directly. (Do you really expect poor hypothetical bit *p = 1; p++
to return 1.125?)
However, you can use bitwise operators, such as <<
, >>
, |
and &
to access a specific bit within a byte.
Solution 3
No, but you can write a function to read the bits one by one:
int readBit(char *byteData, int bitOffset)
{
const int wholeBytes = bitOffset / 8;
const int remainingBits = bitOffset % 8;
return (byteData[wholeBytes] >> remainingBits) & 1;
//or if you want most significant bit to be 0
//return (byteData[wholeBytes] >> (7-remainingBits)) & 1;
}
Usage:
char *data = any memory you like.
int bitPointer=0;
int bit0 = readBit(data, bitPointer);
bitPointer++;
int bit1 = readBit(data, bitPointer);
bitPointer++;
int bit2 = readBit(data, bitPointer);
Of course if this kind of function had general value it would probably already exist. Operating bit-by-bit is just so inefficient compared to using bit masks, and shifts etc.
Solution 4
Conceptually, a "bit pointer" is not a single scalar, but an ordered pair consisting of a byte pointer and a bit index within that byte. You can represent this with a structure containing both, or with two separate objects. Performing arithmetic on them requires some modular reduction on your part; for example, if you want to access the bit 10 bits past a given bit, you have to add 10 to the bit index, then reduce it modulo 8, and increment the byte pointer part appropriately.
Incidentally, on historical systems that only had word-addressable memory, not byte-addressable, char *
consisted of a word pointer and a byte index within the word. This is the exact same concept. The difference is that, while C provides char *
even on machines without byte-addressable memory, it does not provide any built-in "bit pointer" type. You have to create it yourself if you want it.
Solution 5
I don't think that is possible since modern computers are byte addressable which means that there is one address for each byte. 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.
If you really want it you could write a class that uses a char* to keep track of the address in memory, a char(or short/int however the value would never need to be higher than 0000 0111 so a char would reduce the memory footprint) to keep track of which bit in that byte you are at and then overload the operators so that it functions as you want it to.
![ashrafi iqbal](https://i.stack.imgur.com/87gJu.jpg?s=256&g=1)
Comments
-
ashrafi iqbal about 2 years
As we know a in c-language char pointer traverse memory byte by byte i.e. 1 byte each time, and integer pointer 4 byte each time(in gcc compiler), 2 byte each time(in TC compiler).
for example:
char *cptr; // if this points to 0x100 cptr++; // now it points to 0x101 int *iptr; // if this points to 0x100 iptr++; // now it points to 0x104
My question is:
How to create a bit pointer in c which on incrementing traverse memory bit by bit?
-
Spidey over 11 yearsYou'll have to use C++ to encapsulate the implementation and abstract the de-referentiation operator. You could do the same in C, but you'll have to use functions to simulate operators.
-
Lundin over 11 yearsIt doesn't make any sense to have a pointer to a bit, because almost every (all?) CPUs work on byte level. The address bus of every CPU I have ever heard of expresses addresses in bytes. Since a pointer is an address, it will have to point to a byte, or larger.
-
-
jkerian over 11 yearsThis wouldn't even be too hard to do in C++, but if you're limited to C, you can't redefine enough of the syntax to make it sugary.
-
R.. GitHub STOP HELPING ICE over 11 yearsUhg, I can do without the syntactic vinegar. There's nothing ugly about doing it in the straightforward way in C.
-
ashrafi iqbal over 11 yearsthanx alot Weston. i hope this will be usful for me.
-
ashrafi iqbal over 11 yearsDear H2CO3 will u plz tell me how to access specific bit within a byte using bitwise operators, such as <<, >>, | and &.
-
Admin over 11 years@ashrafiiqbal For example,
value & (1 << 5)
checks if the 6th bit is set in a bitmask, but why don't you google "C bitwise operations"? -
hari over 11 years@R.. Very well explained. Thank you for the history too.
-
hari over 11 years@ashrafiiqbal I believe you have enough help provided by ppl :-) Try it out and post your code if you still have issues.
-
Marc over 6 yearsIs this me or does the commented MSB variant not work? I had to make that shift part 7 instead of 8 to get correct results. So for me it would be:
//return (byteData[wholeBytes] >> (7-remainingBits)) & 1;
-
Marc over 6 years@weston Haha thanks. Can you explain why? I can't figure out why I need to use 7 instead of 8 to make it work :O (although i do seem to understand bitwise operations and shifting a bit ^^). Does it make sense to you?
-
weston over 6 years@Marc Sure,
remainingBits
is a number between 0 and 7 inclusive because the number is calculated as modulus of 8. We are trying to reverse that to go from 7 to 0.7-remainingBits
does exactly that. -
weston over 6 yearsIf we use 8, when we get a zero for
remainingBits
we would end up shifting by 8, which is the whole byte gone. We'd overshoot. -
Marc over 6 yearsThanks! I understand now :)