C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
t
                                                               y
         Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.


                                                               C
                                       Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS UNIONS, BITWISE,
BIT-FIELDS AND ENUM
                                   2
Unions
• A variable that may hold variables of different
  types and sizes.
• Only contains one data member at a time.
• Member of union share same space.
• Last data member defined can be accessed.




                        3
Unions
• Unions declaration
       union u_tag {
               int ival;
               float fval;
               char cval;
       }u;
  – Use union keyword to declare a union.
  – u_tag is the name of union.
  – ival, fval & cval are the member of the u_tag
    union.

                             4
Union…
• All operation applied on structures can be
  applied on unions.
• Access:
  – Union_name.member;
  – Union_pointer -> member;
• Unions may occur within structures and arrays,
  and vice versa.

                         5
Bitwise-operators
• All data represent as sequence of bits.
• A bit can be zero or one.
• C provides some operators to work on bits.




                        6
Bitwise-operators
• & (Bitwise AND)
  – Set 1 if both operands are 1.
    A       B     A &B                  011 3
    0       0       0               &   101 5
    0       1       0                   001 1
    1       0       0
    1       1       1




                            7
Bitwise-operators
• I (Bitwise OR)
  – Set 1 if any one operand is 1.
    A       B      A|B                   011 3
     0      0       0                |   101 5
     0      1       1                    111 7
     1      0       1
     1      1       1




                            8
Bitwise-operators
• ^ (Bitwise Exclusive-OR)
  – Set 1 if exactly one operand is 1.
    A       B      A^B                       011 3
    0       0       0                    ^   101 5
    0       1       1                        110 6
    1       0       1
    1       1       0




                             9
Bitwise-operators
• << (left-Shift)
  – Shifts the bits of the first operand left by the
    number of bits specified by the second operand; fill
    from the right with 0 bits.
        5    0   0   1   0   1
  – 5 << 1
             0   1   0   1   0        10


                                 10
Bitwise-operators
• >> (Right-Shift)
  – Shifts the bits of the first operand right by the
    number of bits specified by the second operand.
        5     0   0   1   0   1
  – 5 >> 1
              0   0   0   1   0    2



                              11
Bitwise-operators
• ~ (One’s Complement)
  – All 0 bits are set to 1 and all 1 bits are set to 0.
    A       ~A
    0        1
    1        0




                              12
Bitwise-Assignment operator
•   &= (Bitwise AND assignment operator)
•   |= (Bitwise OR assignment operator)
•   ^= (Bitwise Exclusive OR assignment operator)
•   <<= (Left-shift assignment operator)
•   >>= (Right-shift assignment operator)




                            13
Operator                                 Associativity Type
() [] . ->                               left to right   Highest
+ - ++ -- ! & * ~ sizeof (type)          right to left   Unary
* / %                                    left to right   multiplicative
+ -                                      left to right   additive
<< >>                                    left to right   shifting
< <= > >=                                left to right   relational
== !=                                    left to right   equality
&                                        left to right   bitwise AND
^                                        left to right   bitwise OR
|                                        left to right   bitwise OR
&&                                       left to right   logical AND
||                                       left to right   logical OR
?:                                       right to left   conditional
= += -= *= /= &= |= ^= <<= >>= %=        right to left   assignment
,                                        left to right   comma

                                    14
Bit-fields
• Member variable of a structure with the
  specified bits size.
        struct dob{
                 unsigned int dd : 5;
                 unsigned int mm : 4;
        };

• Better memory utilization.
• int / unsigned;

                            15
Unnamed bit-field
• Used as the padding.
        struct rent{
                 unsigned int x : 5;
                 unsigned int : 4;
                 unsigned int y : 4;
        };




                              16
Enumeration
• Integer constants represented by identifiers.
       enum days { sun, mon, tue, wed, thu, fri, sat }

• It creates a new data type (days).
• Values start at 0 and increment by 1.
• If you want to set the start value write as:-
      enum days { sun = 1, mon, tue, wed, thu, fri, sat }




                               17
Enumeration…
• To use inside program declare variable like:-
       enum days day;

• OR, use typedef
       enum days Day;
       Day day;




                         18
To get complete benefit of this tutorial solve all the quiz on
                       www.learnbywatch.com

              For any problem in this tutorial mail me at
                    yogendra@learnbywatch.com
                        with the subject “C”

                     For Other information mail at
                       info@learnbywatch.com


Keep Watching Keep Learning
NEXT IS FILE I/O


                                    19

Unions

  • 1.
    C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  • 2.
    t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS UNIONS, BITWISE, BIT-FIELDS AND ENUM 2
  • 3.
    Unions • A variablethat may hold variables of different types and sizes. • Only contains one data member at a time. • Member of union share same space. • Last data member defined can be accessed. 3
  • 4.
    Unions • Unions declaration union u_tag { int ival; float fval; char cval; }u; – Use union keyword to declare a union. – u_tag is the name of union. – ival, fval & cval are the member of the u_tag union. 4
  • 5.
    Union… • All operationapplied on structures can be applied on unions. • Access: – Union_name.member; – Union_pointer -> member; • Unions may occur within structures and arrays, and vice versa. 5
  • 6.
    Bitwise-operators • All datarepresent as sequence of bits. • A bit can be zero or one. • C provides some operators to work on bits. 6
  • 7.
    Bitwise-operators • & (BitwiseAND) – Set 1 if both operands are 1. A B A &B 011 3 0 0 0 & 101 5 0 1 0 001 1 1 0 0 1 1 1 7
  • 8.
    Bitwise-operators • I (BitwiseOR) – Set 1 if any one operand is 1. A B A|B 011 3 0 0 0 | 101 5 0 1 1 111 7 1 0 1 1 1 1 8
  • 9.
    Bitwise-operators • ^ (BitwiseExclusive-OR) – Set 1 if exactly one operand is 1. A B A^B 011 3 0 0 0 ^ 101 5 0 1 1 110 6 1 0 1 1 1 0 9
  • 10.
    Bitwise-operators • << (left-Shift) – Shifts the bits of the first operand left by the number of bits specified by the second operand; fill from the right with 0 bits. 5 0 0 1 0 1 – 5 << 1 0 1 0 1 0 10 10
  • 11.
    Bitwise-operators • >> (Right-Shift) – Shifts the bits of the first operand right by the number of bits specified by the second operand. 5 0 0 1 0 1 – 5 >> 1 0 0 0 1 0 2 11
  • 12.
    Bitwise-operators • ~ (One’sComplement) – All 0 bits are set to 1 and all 1 bits are set to 0. A ~A 0 1 1 0 12
  • 13.
    Bitwise-Assignment operator • &= (Bitwise AND assignment operator) • |= (Bitwise OR assignment operator) • ^= (Bitwise Exclusive OR assignment operator) • <<= (Left-shift assignment operator) • >>= (Right-shift assignment operator) 13
  • 14.
    Operator Associativity Type () [] . -> left to right Highest + - ++ -- ! & * ~ sizeof (type) right to left Unary * / % left to right multiplicative + - left to right additive << >> left to right shifting < <= > >= left to right relational == != left to right equality & left to right bitwise AND ^ left to right bitwise OR | left to right bitwise OR && left to right logical AND || left to right logical OR ?: right to left conditional = += -= *= /= &= |= ^= <<= >>= %= right to left assignment , left to right comma 14
  • 15.
    Bit-fields • Member variableof a structure with the specified bits size. struct dob{ unsigned int dd : 5; unsigned int mm : 4; }; • Better memory utilization. • int / unsigned; 15
  • 16.
    Unnamed bit-field • Usedas the padding. struct rent{ unsigned int x : 5; unsigned int : 4; unsigned int y : 4; }; 16
  • 17.
    Enumeration • Integer constantsrepresented by identifiers. enum days { sun, mon, tue, wed, thu, fri, sat } • It creates a new data type (days). • Values start at 0 and increment by 1. • If you want to set the start value write as:- enum days { sun = 1, mon, tue, wed, thu, fri, sat } 17
  • 18.
    Enumeration… • To useinside program declare variable like:- enum days day; • OR, use typedef enum days Day; Day day; 18
  • 19.
    To get completebenefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS FILE I/O 19