Pages

Hello there!

Powered by Blogger.

Monday, January 7, 2013

BITWISE OPERATORS IN C


BITWISE OPERATORS IN C WITH EXAMPLE


Bitwise used for manipulation of data in bit level.
OperatorFunction
`bitwise NOT
<<left shift
>>right shift
&bitwise AND
^bitwise XOR
|bitwise OR
&=bitwise AND assign
^=bitwise XOR assign
|=bitwise OR assign

Bitwise not applicable for float, double and char.

Syntax:-

syntax for & | and ^

value1 bitwise value2

Example:-
2 & 3
num1 & num2

Here value1 and value2 must be integer data type


syntax for >> and <<

value bitwise number_of_shift

Example:-
6 >> 2
num >> 3

Here value and number_of_shift  must be integer data type

Note:-
# Left shift by 2 is equal to multiply by 2
# Right shift by 2 is equal to division by 2


Example:-

Program for shift left


Output:-
Enter a number
5
answer: 20

5 is in binary 101 when we shift left we put zero at right side. Here we shifting by 2 so we put 2 zero at right side 10100 ie 20

LOGICAL OPERATORS IN C


LOGICAL OPERATORS IN C WITH EXAMPLE



Operator

.




Meaning
<less than
<=less than or equal to
>greater than
>=greater than or equal to
==equal to
!=not equal to
&&logical AND
||logical OR
!logical NOT

The above 1st 6 are relational operators in java and last 3 are logical operators

The relation operators are used to compare 2 values. The output of relational operators is either true (1) or false (0)

Syntax:-

value1 relational_operator value2


Example:-

2 < 10     True
3 > 8       False
7.5>=5    True

value1 and value2 are can be constants, variable or combination of them.

Relation operator are used in if-else or in loop statement.

Example:-

Program to find Greatest number between 2


Logical operators:-

logical operators && and || are used to when we want to check 2 or more condition statement.

Example

8 > 7 && 5 < 8      True
8 < 5 || 8 < 9           False
7 > 2 && 19 < 2    False

AND (&&) operator output is true only when either condition statement is true.
OR (||) operator output is false only when either condition statement is false.

Truth table:-


ABAND
000
010
100
111


ABOR
000
011
101
111



NOT (!)  logical operator is used for compliment condition statement outputs.

Truth table:-

ANOT
01
10


Example:-

Program to solve following problem
The candidate is selected if his age and height is 18 and 5 and more respectively.



Java program for logical NOT
Program to find max number between 2


ARITHMETIC OPERATORS IN C


ARITHMETIC OPERATORS IN C WITH EXAMPLE

Different Arithmetic operators used c are shown in table.

Operatorfunction
*Multiplication
/Division
%Remainder
+Addition
-Subtraction

* operator is used give product of 2 numbers.

/ Operator gives Quotient after dividing Divisor and Dividend. Divisor should be nonzero number. 

% operator gives remainder after division of 2 numbers. % operator can be used only for integer data type.

+ operator gives sum of 2 numbers.

- operator used to subtract one number from another number. 

Note:-

# If both number are positive than answer will be in positive number.
# If either or both number are negative than answer will be either in positive or in negative, it is machine dependent.

Example:-

Program to find sum of 2 number.



Program to find product of 2 number.


Program to find subtract 2 number.


Program to find division 2 number.



Program to find remainder.



LIST OF KEYWORD USED IN C



LIST OF KEYWORD USED IN C

ALL KEYWORDS HAVE FIXED MEANING AND MEANING CAN`T BE CHANGED. KEYWORD ARE BASIC BUILDING BLOCKS FOR PROGRAM.


list of keyword used in c
autodoubleintstruct
breackelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile


Note:-

# Keyword can`t be used as variable name or class name.