Friday, April 27, 2012

use of volatile keyword and constant


Volatile is the used to tell the compiler to modify at any instance ;
constant is used to tell the compiler the value is not going to be changed
constant volatile both is used in same variable it indicate that the variable
is not modified through the program
' but it modified by external interrupts are inputs other than via program
this confirm that the program not modified using program.

Set and clear bit using MACRO


#include "<"stdio.h">"
#define CLRBIT(byte,_bit) byte&=~(1<<_bit)//clear bit in a byte _bit is the position want to clear
#define SETBIT(byte,_bit) byte|=(1<<_bit)
main()
{ unsigned char byte=0xff;
printf("\n The value before clear=0x%x",byte);
byte=CLRBIT(byte,1);
printf("\n The value after clear=0x%x",byte);
byte=SETBIT(byte,1);
printf("\n The value after set=0x%x",byte);
} //output
//The value before clear=0xff;
//The value after clear=0xfd;
//The value after set=0xff;

Bitfield inside union or Accessing bitfield inside union


// This is the program to access bits using union and structure #include "<"stdio.h">"
union _bitf
{
unsigned int _byte;
struct bitacc
{
unsigned char _bit0:1;
unsigned char _bit1:1;
unsigned char _bit2:1;
unsigned char _bit3:1;
unsigned char _bit4:1;
unsigned char _bit5:1;
unsigned char _bit6:1;
unsigned char _bit7:1;
unsigned char _bit8:1;
unsigned char _bit9:1;
unsigned char _bit10:1;
unsigned char _bit11:1;
unsigned char _bit12:1;
unsigned char _bit13:1;
unsigned char _bit14:1;
unsigned char _bit15:1;
} stracc;
} access;
int main()
{
printf("\n---0x%x",access._byte);
access.stracc._bit0=1;
access.stracc._bit3=0;
printf("\n---0x%x",access._byte);
access.stracc._bit4=0;
access.stracc._bit7=0;
printf("\n---0x%x",access._byte);
access.stracc._bit11=0;
access.stracc._bit15=1;
printf("\n---0x%x",access._byte);
if(access._byte==0x8001)
printf("\n It is possible");
}
/*This program is created to use bitfield and check the flag commonly */
using this you can reduce the variable no . and decrease the size of the variable.