BItwiseOperatorStructure
BItwiseOperatorStructure
Computer Programming
Bitwise operators and Structure
Dr. Tanveer Awal
Department of CSE, BUET
Bitwise Operators
• Performs operation on a bit-by-bit level.
Operator Type Name
• So, i | j results in 11
bitwise_intro.c
Bitwise Operators
• Bitwise XOR
unsigned int i, j;
i=11
j=1
• Determine i ^ j
00000000 00000000 00000000 00001011
00000000 00000000 00000000 00000001
------------------------------------------------------------------------
00000000 00000000 00000000 00001010
• So, i ^ j results in 10
bitwise_intro.c
Bitwise Operators
• Bitwise NOT
unsigned int i, j;
i=11
• Determine ~i
00000000 00000000 00000000 00001011
------------------------------------------------------------------------
11111111 11111111 11111111 11110100
bitwise_intro.c
Shift Operators
bitwise_shift_operator1 &2.c
Bitwise Operators
• The left shift and right shift operators should not be used for negative numbers.
• The bitwise operators should not be used in place of logical operators.
• The left-shift and right-shift operators are equivalent to multiplication and
division by 2 respectively.
• The & operator can be used to quickly check if a number is odd or even.
• The ~ operator should be used carefully.
bitwise_intro.c
Structure
• Aggregate data type
• Composed of two or more related variables
– Called member
– Each member can be of different types
Structure (General Form)
struct tag-name {
type member1;
type member2;
type member3;
.
.
.
type memberN;
} variable-list;
struct {
int x;
int y;
} p1, p2;
Structure Example
struct point {
int x;
int y;
};
struct point p1, p2;
• Keyword struct before variable declaration is necessary
• Each instance of a structure contains its own copy of the members
• Structure declaration without any variable name does not reserve any storage
– Describes template or shape of a structure
Structure Declaration
• Structure can be declared
– Locally (inside a function)
– Globally (outside of any function)
See structure_in_function.c
Pointer to Structure
• Pointer of a structure can be declared in the same way pointers to other
variables are declared.
• When accessing a member using a structure, use the dot operator. When
accessing a member using a pointer, use the arrow operator.
• See “structure_pointer.c” and “structure_pointer1.c” for the use of pointer
to structure
• See structure_pointer2.c to see the use of pointer for the case of nested
structure
typedef (user defined data types)
• typedef type new-type
• type: an existing data type
• Example:
– typedef int age
• age is a user defined data type, equivalent to int
– typedef double height;
height maleHeight, femaleHeight;
typedef with Structure
typedef struct
{
member1;
member2;
} new-type;
• typedef struct {
int month;
int day;
int year;
} date;
typedef with Structure
See structure_typedef.c
Bit-field
• A variation on structure member.
• Composed of 1 or more bits.
• type name: size;
• type can be int or unsigned int
• struct b_type
{ unsigned dept:3;
unsigned instock:1;
unsigned backOrdered:1;
unsigned lead_time:3;
}inv[MAX_ITEM];
}
Bit-field
• Address of a bit-field variable cannot be obtained. Smallest addressable unit
of memory is a byte.
• Use:
– Pack information into smallest possible space. Hence they allow efficient use of
memory.
• Whether bitfields are stored high order to low order or the other way
around is implementation dependent.
Union
• A single piece of memory shared by 2 or more different variables.
• Only 1 variable may be in use at any time.
• Defined much like structure.
union tag-name {
type member1;
type member2;
type member3;
.
.
.
type memberN;
} variable-list;
Union
union u_type {
int i; Size of union is fixed at compile time and is
char c[4]; large enough to accommodate the largest
member of the union.
double d;
} sample;
i d