• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Unions
 

Unions

on

  • 1,359 views

 

Statistics

Views

Total Views
1,359
Views on SlideShare
1,254
Embed Views
105

Actions

Likes
1
Downloads
86
Comments
0

4 Embeds 105

http://www.ustudy.in 68
http://en.learnbywatch.com 31
http://ustudy.in 4
http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Unions Unions Presentation Transcript

    • 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