Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Decimal to Binary Table

Bitwise Operators in C Programming


In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition,
subtraction, multiplication and division are done in bit-level. To perform bit-level
operations in C programming, bitwise operators are used.

Bitwise AND operator &

The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit
of an operand is 0, the result of corresponding bit is evaluated to 0.

Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25


00001100
& 00011001
________
00001000 = 8 (In decimal)

Bitwise OR operator |

The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C


Programming, bitwise OR operator is denoted by |.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25


00001100
| 00011001
________
00011101 = 29 (In decimal)

Bitwise XOR (exclusive OR) operator ^

The result of bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25


00001100
^ 00011001
________
00010101 = 21 (In decimal)
Flowchart

Flowchart is a pictorial representation of an algorithm. Uses symbols (boxes of different


shapes) that have standardized meaning to denote different types of instructions. Actual
instructions are written in the boxes. Boxes are connected by solid lines having arrow to
indicate the exact sequence in which the instructions are to be executed.

Flowchart to find larger from 3 numbers :-


Algorithms

Write an algorithm to find the largest among three different numbers


entered by user.

Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
What is Programming?
Programming means creating a set of instructions for completing some specific
task. In the context of computing, programming means creating a set of
instructions not for a person but for a computer, in order to accomplish a specific
task. A computer requires programs to function. The program has an executable
form that the computer can use directly to execute the instructions. A collection of
computer programs and related data is referred to as the software. Computer source
code is typically written by computer programmers. Source code may be converted
into an executable file by a compiler and later executed by a central processing
unit. Alternatively, computer programs may be executed with the aid of
an interpreter.
Basic Programming Concepts
Even though each programming language you use is unique, there are certain
concepts common to all languages. Let's look at three of the most common
concepts and structures used in programming.

1. Sequence of commands (The right commands in the right order.):-It is


important not only to give the right commands or steps—they must also be
given in the correct sequence. An excellent example is addressing letters for
mailing.
First name Last name
House number Street name
City, State Zip code
Country
2. Conditional structures (Do certain things based on a true or false, yes or no
decision.):- These provide for one outcome or sequence of events to be
executed if a statement is true, and another outcome or sequence of events to
be triggered if the statement is false. In most programming languages these
structures take the form if . . . then . . . else.
If a is greater then b, then print “a is larger”,
Else Print “B is larger” .
3. Looping structures (A list of instructions to do more than once.)
Used to make the computer repeat a certain command or sequence of
commands. The loop may run for a predetermined number of times, until a
certain condition becomes true, or as long as a certain condition remains true.
Here are some ways that looping might be done:
Do the following 20 times.
Repeat the following until the user presses the option key

C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction
and multiplication on numerical values (constants and variables).

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

remainder after
%
division( modulo division)

Example 1: Arithmetic Operators


// C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);

c = a-b;
printf("a-b = %d \n",c);

c = a*b;
printf("a*b = %d \n",c);

c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision
making in C programming.

Operator Meaning of Operator Example

Logial AND. True only if all If c = 5 and d = 2 then, expression ((c ==


&&
operands are true 5) && (d > 5)) equals to 0 (False).

Logical OR. True only if If c = 5 and d = 2 then, expression ((c ==


||
either one operand is true 5) || (d > 5)) equals to 1 (True).

Logical NOT. True only if the If c = 5 then, expression ! (c == 5) equals


!
operand is 0 to 0 (False).
if Statement
The syntax of the if statement in C programming is:

if (test expression)

// statements to be executed if the test expression is true

The if statement evaluates the test expression inside the parenthesis ().

 If the test expression is evaluated to true, statements inside the body of if are
executed.
 If the test expression is evaluated to false, statements inside the body of if are
not executed.

test expression is evaluated to true (non-zero value) and false (0).


Nested if...else
It is possible to include an if...else statement inside the body of another if...else
statement that is known as nested if…else.

if (test expression -1)

if (test expression-2)

Example of Nested IFstatement -

#include <stdio.h>
void main()
{ int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2)
{
if (number1 == number2)
{
printf("Both are Equal”);
}
else
{
printf("Larger is- %d", number1);
}
}
else
{
printf("Larger is -", number2);
}
}

If the body of an if...else statement has only one statement, you do not need to use
brackets {}.

You might also like