A
Allan Bruce
Hi there,
Would the following functions work for bit manipulation? I want to specify
a byte and the bit of the byte (from the MSB down to LSB) and either set it,
clear it or get what it is currently at.
Thanks
Allan
void BitSet(unsigned char *Byte, int Bit)
{
unsigned char Mask = 0x80; // all zeros with a 1 at the MSB
if (Bit>7 || Byte==NULL)
return;
Mask = Mask >> Bit;
*Byte = *Byte | Mask;
}
void BitClr(unsigned char *Byte, int Bit)
{
unsigned char Mask = 0x80; // all zeros with a 1 at the MSB
if (Bit>7 || Byte==NULL)
return;
Mask = ~(Mask >> Bit);
*Byte = *Byte & Mask;
}
int BitGet(unsigned char *Byte, int Bit)
{
unsigned char Mask = 0x80; // all zeros with a 1 at the MSB
if (Bit>7 || Byte==NULL)
return;
Mask = Mask >> Bit;
if ((*Byte & Mask) == Mask)
return 1;
else
return 0;
}
--
Allan Bruce
Dept. of Computing Science
University of Aberdeen
Aberdeen AB24 3UE
Scotland, UK
Would the following functions work for bit manipulation? I want to specify
a byte and the bit of the byte (from the MSB down to LSB) and either set it,
clear it or get what it is currently at.
Thanks
Allan
void BitSet(unsigned char *Byte, int Bit)
{
unsigned char Mask = 0x80; // all zeros with a 1 at the MSB
if (Bit>7 || Byte==NULL)
return;
Mask = Mask >> Bit;
*Byte = *Byte | Mask;
}
void BitClr(unsigned char *Byte, int Bit)
{
unsigned char Mask = 0x80; // all zeros with a 1 at the MSB
if (Bit>7 || Byte==NULL)
return;
Mask = ~(Mask >> Bit);
*Byte = *Byte & Mask;
}
int BitGet(unsigned char *Byte, int Bit)
{
unsigned char Mask = 0x80; // all zeros with a 1 at the MSB
if (Bit>7 || Byte==NULL)
return;
Mask = Mask >> Bit;
if ((*Byte & Mask) == Mask)
return 1;
else
return 0;
}
--
Allan Bruce
Dept. of Computing Science
University of Aberdeen
Aberdeen AB24 3UE
Scotland, UK