Thursday, 21 September 2017

ABAP: Bit wise operators

Bit wise operators are used to perform bit wise operations between variables. Below table summarizes the bit wise operators provided by ABAP.

ABAP provides BIT-NOT, BIT-AND, BIT-XOR, BIT-OR operators to perform bit level operations.

BIT-NOT Operator
Unary operator that flips all the bits in a hexadecimal number to the opposite value.

BIT-AND, BIT-OR, BIT-XOR operators
Bit1
Bit2
BIT-AND
BIT-OR
BIT-XOR
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA: var1 TYPE X VALUE 8,
      var2 TYPE X VALUE 9,
      result TYPE X.

result = var1 BIT-AND var2.
Write result.

result = var1 BIT-OR var2.
Write / result.

result = var1 BIT-XOR var2.
Write / result.

result = BIT-NOT var1.
Write / result.



Previous                                                 Next                                                 Home

ABAP: Logical Operators

Logical operators are used to combine the conditions. Below table summarizes the logical operators supported by ABAP.

Operator
Description
AND
condition1 AND condition2: Return true if both the conditions are true, else false.
OR
condition1 OR condition2: Return true if either of the conditions is true, else false.
NOT
Not condition : return true, if the condition is false, false if the condition is true.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA var1 TYPE I VALUE 10.
DATA var2 TYPE I VALUE 20.

IF var1 > 5 AND var2 > 5.
  Write / 'var1 and var2 are > 5'.
ENDIF.

IF NOT ( var1 > 20 ).
  Write / 'var1 is not > 20'.
ENDIF.

IF ( var1 > 15 OR var2 > 15 ).
  Write / 'var1 or var2 are > 15'.
ENDIF.

IF ( var1 > 15 AND var2 > 15 ).
  Write / 'var1 or var2 are > 15'.
ENDIF.



Previous                                                 Next                                                 Home

ABAP: Comparison Operators

Below table summarizes the comparison operators supported by ABAP.

Operator
Description
= or EQ
Return true, if two variables are equal, else false
<> or NE
Return true, if two variables are not equal, else false
> or GT
var1 > var2. Return true, if var1 is greater than var2, else false
>= or GE
var1 > var2. Return true, if var1 is greater than or equal to var2, else false
< or LT
var1 > var2. Return true, if var1 is less than var2, else false
<= or LE
var1 > var2. Return true, if var1 is less than or equal to var2, else false
BETWEEN AND
‘var1 BETWEEN var2 AND var3’. Return true, if var1 is in between var2 and var2, else false.
IS INITIAL
The condition becomes true if the contents of the variable have not changed and it has been automatically assigned its initial value.
IS NOT INITIAL
The condition becomes true if the contents of the variable have changed.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA: var1 TYPE I VALUE 25,
      var2 TYPE I VALUE 10,
      var3 TYPE I VALUE 30,
      var4 TYPE I.

IF var1 = var2.
  Write 'var1 is equal to var2'.
ENDIF.

IF var1 <> var2.
  Write 'var1 is not equal to var2'.
ENDIF.

IF var1 < var2.
  Write / 'var1 is < var2'.
ENDIF.

IF var1 > var2.
  Write / 'var1 is > var2'.
ENDIF.

IF var1 <= var2.
  Write / 'var1 is <= var2'.
ENDIF.

IF var1 >= var2.
  Write / 'var1 is >= var2'.
ENDIF.

IF var2 BETWEEN var1 AND var3.
  Write / 'var2 is in between var and var3'.
ENDIF.

IF var1 IS INITIAL.
  Write / 'var1 is initialized with default value'.
ENDIF.

IF var4 IS INITIAL.
   Write / 'var4 is initialized wth default value'.
ENDIF.



Previous                                                 Next                                                 Home

ABAP: Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations between operands.

Operator
Description
+
Perform addition
-
Perform subtraction
*
Perform Multiplication
/
Perform Division
mod
Gives remainder. Ex: operand1 MOD operand2

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA: var1 TYPE I VALUE 25,
      var2 TYPE I VALUE 10,
      result TYPE I.

result = var1 + var2.
Write / result.

result = var1 - var2.
Write / result.

result = var1 * var2.
Write / result.

result = var1 / var2.
Write / result.

result = var1 mod var2.
Write / result.



Previous                                                 Next                                                 Home

ABAP: Operators

Operators are used to perform operation on operands. Operators are categorized like below.

a.   Arithmetic Operators
b.   Comparison Operators
c.   Bitwise Operators
d.   Logical Operators
e.   String Operators
f.    Assignment Operator




Previous                                                 Next                                                 Home

ABAP: Defining Structured variables

In my previous post, I explained how to define variables (by using basic types of ABAP). We can also define complex variables by combining these primitive variables.

Employee has first_name, last_name, age, address. Following statements are used to created Employee structure.

TYPES: BEGIN OF Employee,
      first_name TYPE STRING,
      last_name TYPE STRING,
      age TYPE I,
      address TYPE STRING,
      END OF EMPLOYEE.

How to define a variable of type Employee?
It is just like defining normal variables.
DATA emp1 TYPE Employee.

Above statement creates a variable of type Employee.

How to access the variables inside emp1?
The fields of structured variables are accessed using ‘-‘.
emp1-first_name = 'Hari Krishna'.

Above statement sets the value ‘Hari Krishna’ to the field ‘first_name’ of the variable emp1.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

* Defining Variables
TYPES: BEGIN OF Employee,
      first_name TYPE STRING,
      last_name TYPE STRING,
      age TYPE I,
      address TYPE STRING,
      END OF EMPLOYEE.

DATA emp1 TYPE Employee.
emp1-first_name = 'Hari Krishna'.
emp1-last_name = 'Gurram'.
emp1-age = 28.
emp1-address = 'Bangalore'.

Write emp1-first_name.
Write / emp1-last_name.
Write / emp1-age.
Write / emp1-address.



Previous                                                 Next                                                 Home

ABAP: Define variable using LIKE keyword

If you already defined a variable, you can define other variable by referring the existing one.

Ex:
DATA: first_name TYPE STRING,
      last_name LIKE first_name.

As you see above snippet, last_name is declared by using LIKE operator.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

* Defining Variables
DATA: first_name TYPE STRING,
      last_name LIKE first_name,
      age TYPE I.

first_name = 'Hari Krishna'.
last_name = 'Gurram'.
age = 28.

Write first_name.
Write / last_name.
Write / age.



Previous                                                 Next                                                 Home