0% found this document useful (0 votes)
1 views

BItwiseOperatorStructure

Uploaded by

aritradash0020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

BItwiseOperatorStructure

Uploaded by

aritradash0020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

CSE 109

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

& Binary Bitwise AND


| Binary Bitwise OR
^ Binary Bitwise XOR
~ Unary 1’s Complement
<< Binary Left Shift Operator
>> Binary Right Shift Operator

• Works with character type and integer types only.


Bitwise Operators
• Bitwise AND
unsigned int i, j;
i=11
j=1
• Determine i & j
00000000 00000000 00000000 00001011
00000000 00000000 00000000 00000001
------------------------------------------------------------------------
00000000 00000000 00000000 00000001

• So, i & j results in 1


bitwise_intro.c
Bitwise Operators
• Bitwise OR
unsigned int i, j;
i=11
j=1
• Determine i | j
00000000 00000000 00000000 00001011
00000000 00000000 00000000 00000001
------------------------------------------------------------------------
00000000 00000000 00000000 00001011

• 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;

– The keyword struct means that a structure type is defined


– tag-name is the name of the type
– Either tag-name or variable-list is optional
Structure Example
struct point {
int x;
int y;
} p1, p2;

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)

• Structure declaration and structure variable declaration are different.


– Variables can be declared during structure declaration or later

• It is possible to declare structure globally and declare the variables of the


structure locally

• See structure_declaration.c for detailed declaration.


Structure Initialization
p1.x=10;
p1.y=5;

struct point p3={5, 2};


Structure Size
• The size of a structure element is greater than or equal to the summation of
sizes of its fields (members).
• Why may it be greater?
– For the ease of memory access! Memory is not accessed bytewise rather accessed
4 bytes per operation or 8 bytes per operation. So, it is common to find the size
of the structure as the multiple of 4 or 8.
– Memory access is higher topic, not for now. Just remember the reason behind
larger structure size.
Structure Assignment
• Possible when type of the both objects are same
p2=p3;
Accessing Structure Member
• Use dot operator to access the member of a structure
StructureVariable.member
For example, if p3 is a structure variable,
printf("%d, %d\n", p3.x, p3.y);
• For scanf or other cases where address of a member is required, use &
operator before structure variable name not before member name.
&StructureVariable.member
For example, if p3 is a structure variable,
scanf("%d %d\n", &p3.x, &p3.y);
• See “structure_class_example.c” and “structure_class_example1.c”
Structure Array
• A structure array can be declared like the process of a normal variable
declaration.
• Index must be used to access an element of the array
• To access the member of that element, use dot operator after the index

Also see “structure_class_example2.c”


Structure Array
• The member of a structure and a normal variable in a function can have the
same name.

Member x and int x are different


Nested Structure
• A structure can be used inside another structure and so on.
• If structure A is used inside structure B, then structure A must be declared
before structure B.
• If structure A is used inside structure B, then B is the outer structure and A
is inner structure.
• During accessing the elements of nested structure object, use the outermost
structure variable first, then a dot, then the inner one, and so on.
Nested Structure
Structure in Function
• A structure object can be passed into a function and a structure object can be
returned from a function.
Structure in 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

c[0] c[1] c[2] c[3]


How an instance of u_type appears in memory
(assuming 4 byte int and 8 byte double
Thank You 

You might also like