void main()
{
struct time t;
struct date dt;
getdate(&dt);
gettime(&t);
clrscr();
printf("System Date : %d/%d/%d \n", dt.da_day, dt.da_mon, dt.da_year);
printf("System Time : %u:%u:%u ", t.ti_hour, t.ti_min, t.ti_sec);
getch();
}
BIT FIELDS
C allows using bit fields to hold data items and thereby to pack several data items in a word (16 bits) of memory. Bit fields allow direct manipulation of a string of pre-selected bits as if it represented an integral quantity.
A bit field is a set of adjacent bits whose size can be from 1 to 16 bits in length. A word (16 bits) can therefore be divided into a number of bit fields. The name and size of bit fields are defined using a structure.
Syntax:
struct tag
{
data-type member-1: bit-length;
data-type member-2: bit-length;
…
…
data-type member-n: bit-length;
}
The data-type is either int (signed int) or unsigned int and the bit-length is the number of bits used for the specified member. A signed bit field should have at least 2 bits (one bit for sign). A colon must follow the member name. The bit-length is decided by the range of value to be stored in that field. The largest value that can be stored is 2n–1, where n is the bit-length.
Note: The programmer cannot take the address of a bit field variable. This means we cannot use scanf to read values into bit fields and cannot use pointer to access the bit fields. Bit field cannot be arrayed. Bit fields should be assigned values that are within the range of their size. If you try to assign larger values, unpredicted results will be obtained.
E.g. struct details
{
unsigned sex : 1; /* can store values 1 or 0 */
unsigned age : 7; /* can store values 0 to 127 */
unsigned m_status : 2; /* can store values 0 to 3 */
unsigned employed : 1; /* can store values 1 or 0 */
unsigned children : 3; /* can store values 0 to 7 */
} emp;
In the above example the structure variable emp consumes only two bytes in memory. Once bit fields are defined, they can be referenced just like the normal structure members of type integer using the member operator (dot or arrow).
E.g. emp.sex=1;
emp.age=34;
emp.m_status=2; /* 2 for divorced */
emp.children=3; etc.
Sample program
# include
struct details
{
unsigned sex : 1;
unsigned age : 7;
unsigned m_status : 2;
unsigned employed : 1;
unsigned children : 3;
};
main()
{
struct details emp, *ptr;
int age; char sex;
clrscr();
ptr= &emp;
printf(“Enter age : ”);
scanf(“%d”, &age);
printf(“Enter sex (m/f) : “); fflush(stdin);
scanf(“%c”, &sex);
emp.age = age;
ptr->sex = (sex ==’m’) ? 1: 0;
printf(“Age = %d \n”, emp.age);
printf(“Sex = %s “, (emp.sex == 1) ? “Male” : “Female” );
getch();
}
Every single byte of data is made up of eight bits. Some of the applications (like system software) may need to address and manipulate every single bit separately. One of C’s powerful features is a set of bit manipulation operators, not usually found in high-level languages. These permit the programmer to access and manipulate individual bits within a piece (byte) of data. Bit-level manipulations are used in setting a particular bit or group of bits to 1 or 0, to test a particular bit is set or cleared and to perform certain numerical calculations faster. A simple example is the use of a byte as a status flag, where each or groups of bit(s) indicate some property like bit 0 ON equals “read-only”, bit 6 ON equals “busy”, bits 1-4 represents the count of variables etc.
Bitwise Operators can operate only upon ints and chars but not on floats and doubles. Bits are numbered from zero onwards, increasing from right to left.
7
|
6
|
5
|
4
|
3
|
2
|
1
|
0
|
|
|
|
|
|
|
|
|
Character
|
15
|
14
|
13
|
12
|
11
|
10
|
9
|
8
|
7
|
6
|
5
|
4
|
3
|
2
|
1
|
0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Integer
|
The various Bitwise Operators can be classified into three categories, they are:
Bitwise Negate Operator (One’s Complement Operator)
Bitwise Logical Operators
Bitwise Shift Operators
Share with your friends: