'C' Bitwise Operators. Relevance to 'C' of bitwise applications. Syntax and
expressions. Example getter and setter functions. Eratothene's prime number
sieve.
'C' Bitwise Operators
Relevance to 'C' of bitwise applications Syntax and expressions Example getter and setter functions
Eratothene's prime number sieve By Richard Kay‘s
Relevance to 'C' of Bitwise Applications
'C' was designed to write system software as an alternative to assembler: compilers, kernels, device drivers, interpreters, relational database engines, virtual machines. So this language needs access to raw hardware and individual bit values. Coding designed for specific hardware design features will be non portable.
Portability Constraints
Different microprocessors organise integer and floating point data differently, e.g. big endian or little endian, two's or one's complement, location and size of exponent, sign bit and mantissa. Device drivers for different hardware implement different instruction sets.
Operator Summary
In-place operators
In-place versions of operators exist which modify the LHS operand in place rather than returning the result for a seperate assignment, e.g. a >>= b performs a right shift of b bits directly on a . These work in the same manner as += , -= and *= in-place operators compared to + - and * .
Left and Right Shift Operators
The >> operator shifts a variable to the right and the >3); /* prints 16 divided by 8 */ printf("%d\n",a= MAXBYTES){ fprintf(stderr,"setbit: buffer overflow trapped\n"); exit(1); /* needs #include */ } bit07=(int)bitpos%8; /* remainder value 0 - 7 */ setbitn(sp+byteno,bit07,value); }
getbit function int getbit(unsigned char *sp, size_t bitpos){ /* getbit gets bit position 0 - ((MAXBYTES * 8) - 1) of sp, * returns 0 or 1 */ size_t byteno; int bit07; byteno=bitpos/8; /* floor division to get byte number in string */ if(byteno >= MAXBYTES){ fprintf(stderr,"getbit: read beyond end of buffer \n"); fprintf(stderr,"bitpos: %d\n",bitpos); exit(1); } bit07=(int)bitpos%8; /* remainder value 0 - 7 */ return getbitn(*(sp+byteno),bit07); }
Eratothene's prime number sieve Eratothene's sieve is an algorithm we can use quickly to obtain and store all small prime numbers, e.g. less than 230 .To store these efficiently we need to use a bit array, so that if the bit for the position in the array is a 0 the number for that position is prime and if the bit for this position is a 1 the number for that position is not prime. When we have all the small prime numbers, we can use these to test whether much larger randomly generated numbers are prime so they can be used for generating cryptographic keys.
Eratothene's prime number sieve Positions in this array all start at 0 (meaning the number or index of the position could be prime) except for positions 0 and 1 (which are not prime numbers. Starting with 2 this number is multiplied by 2, 3, 4 etc. to strike out the even numbers starting 4, 6, 8 etc. up to the highest position in the boolean array. These bits are set to 1s, meaning they can't be prime. The next 0 in the array is then found, which is the next prime number 3, and this number is multiplied by all higher numbers, and all multiples are set to 1. This process continues until the first prime greater than the square root of the highest bit position is found. Bitwise operators are needed to implement Eratosthenes Sieve algorithm without wasting memory.
Eratothenes Sieve main function int main(void){ /* from eratosthenes.c , Richard Kay, Sept 2005 */ int i,j,nextprime=2,k; unsigned char a[MAXBYTES]; /* allocate memory for bit array */ for(i=0;i