ashrafi iqbal
ashrafi iqbal

Reputation: 89

how to create pointer to a bit in c-language

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?

Upvotes: 4

Views: 13266

Answers (8)

weston
weston

Reputation: 54781

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.

Upvotes: 3

user5766977
user5766977

Reputation: 1

With pointers it does not look like possible.But to write or read any bit of the data you can try this one.

unsigned char data;

  struct _p

{

unsigned char B0:1;

unsigned char B1:1;

unsigned char B2:1;


unsigned char B3:1;


unsigned char B4:1;


unsigned char B5:1;


unsigned char B6:1;


unsigned char B7:1;

} 

int main()

{


data = 15;


_p * point = ( _p * ) & data;

//you can read and write any bit of the byte with point->BX; ( Ex: printf( "%d" , point->B0;point->B5 = 1;


}

Upvotes: 0

user2848799
user2848799

Reputation: 1

You could always cast your pointer to integer, that is at least 3 bits bigger in size than byte pointer used at the system. Then just shift the pointer after the cast left by 3 bits. Then store the bit information on the least significant 3 bits.

This integer "bitpointer" can then be incremented with normal arithmetic.

Something like this:

#include <stdio.h>

#define bitptr long long
#define create_bitptr(pointer,bit) ((((bitptr)pointer)<<3)|bit) ;
#define get_bit(bptr) ((bptr)&7)
#define get_value(bptr)  (*((char*)((bptr)>>3))) 
#define set_bit(bptr) get_value(bptr) |= 1<<get_bit(bptr)
#define clear_bit(bptr) get_value(bptr) &= (~(1<<get_bit(bptr)))

int main(void)
{
    char variable=0; 

    bitptr p ;
    p=create_bitptr(&variable,0) ;

    set_bit(p) ;    p++ ; //1
    clear_bit(p) ;  p++ ; //0
    set_bit(p) ;   p++ ;  //1
    clear_bit(p) ; p++ ;  //0
    clear_bit(p) ; p++ ;  //0
    clear_bit(p) ; p++ ;  //0
    clear_bit(p) ; p++ ;  //0
    clear_bit(p) ; p++ ;  //0

    printf("%d\n",variable) ;
    return 0;
}

Upvotes: 0

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215261

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.

Upvotes: 3

TheNextBillGates
TheNextBillGates

Reputation: 31

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.

Upvotes: 2

jkerian
jkerian

Reputation: 17016

The char is the 'smallest addressable unit' in C. You can't point directly at something smaller than that (such as a bit).

Upvotes: 8

hari
hari

Reputation: 9733

I am not sure what you are asking is possible. You need to do some magic with bit shifting to traverse through all the bits of a byte pointed by the pointer.

Upvotes: 0

user529758
user529758

Reputation:

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.

Upvotes: 7

Related Questions