/*
Project SetBit
Set any bit in a byte
Version 1.0 / 2002.04.01
Autor: pstrainer@gmx.net
*/
#include
#include
void set_bit(unsigned char*,int,int);
void main (void) {
unsigned char c;
int i,j,bitpos,bitwert;
printf("Projekt SetBit\n\n");
printf("Bit-Position (0...7): ");
scanf("%d",&bitpos);
printf("Bit-Wert (0...1): ");
scanf("%d",&bitwert);
if ( (bitpos<0) || (bitpos>7) || (bitwert<0) || (bitwert>1) )
{printf("Eingabefehler: Keine Funktion von set_bit !\n");}
printf("\nAlle Werte von set_bit(00,%d,%d) bis set_bit(FF,%d,%d)\n",bitpos,bitwert,bitpos,bitwert);
for (i=0;i<16;i++) {
for (j=0;j<16;j++) {
c=i*16+j;
set_bit(&c,bitpos,bitwert);
printf("%3X",c);
}
printf("\n");
}
#ifdef _DEBUG
printf("\nPress any key to continue ");
_getch();
#endif
}
void set_bit(unsigned char *c,int p,int v) {
/*
Funktion set_bit
Setzt ein Bit eines Bytes auf einen gewünschten Wert
Version 1.0 / 2002.04.01
Autor: pstrainer@gmx.net
Syntax:
unsigned char the_byte;
int bitposition, bitvalue;
...
set_bit(&the_byte,bitposition,bitvalue);
Parameter:
the_byte: 0...255
bitposition 0...7
bitvalue: 0 (clear this bit), 1 (set this bit)
Funktion arbeitet bei illegalen Parametern ohne Änderung.
Length:
13 lines of code
*/
unsigned char mask;
if ( (p>=0) && (p<=7) && (v>=0) && (v<=1) ) {
mask = 1 << p; // generate 00001000 type mask
if (v) {
*c = *c | mask; // set bit
}
else {
mask = ~mask; // invert to 11110111 type mask
*c = *c & mask; // clear bit
}
}
}
/* ========== eof ========== */
               (
geocities.com/pstrainer/entwicklung/c)                   (
geocities.com/pstrainer/entwicklung)                   (
geocities.com/pstrainer)