## Friday, November 7, 2014

### Using Bitwise and Bitshift Operators In Java

Introduction:

Today I want to talk about less commonly used and known operators that can be used on integral numbers in Java, Bitwise and Bitshift operators.Bitwise operators most commonly used in masking operations and bitshift operators may be used in encyription or hashing algorithms.I'll try to explain them with some small examples.

Bitwise Operators:

&(Bitwise and) operator: Used to accomplish "and" operation bit by bit.

Example:
int a = 2; //00000000000000000000000000000010 ->32 bit signed(leftmost bit for sign)
int b = 7: //00000000000000000000000000000111 ->32 bit signed(leftmost bit for sign)
int result = a & b;//00000000000000000000000000000010
System.out.println("a & b = " +  result ); // prints 2

Note: All the primitive types are signed in Java.Therefore the max value of an int in java is max 2^31 -1 and min - 2^31.That's because the leftmost bit is used for sign and zero is count in positive numbers that is 0000..0000 is used for zero.

(Bitwise inclusive or)  operator: Used to accomplish "inclusive or" operation bit by bit.

Example:
int a = 2; //00000000000000000000000000000010 ->32 bit signed(leftmost bit for sign)
int b = 7: //00000000000000000000000000000111 ->32 bit signed(leftmost bit for sign)
int result = a | b;//00000000000000000000000000000111
System.out.println("a | b = " +  result); // prints 7

^(Bitwise exclusive or)  operator: Used to accomplish "exclusive or" operation bit by bit.

Example:
int a = 2; //00000000000000000000000000000010 ->32 bit signed(leftmost bit for sign)
int b = 7: //00000000000000000000000000000111 ->32 bit signed(leftmost bit for sign)
int result = a ^ b;//00000000000000000000000000000101
System.out.println("a ^ b = " + result ); // prints 5

~(Bitwise negate)  operator: Used to accomplish "negate" operation bit by bit.

Example:
int a = 2; //00000000000000000000000000000010 ->32 bit signed(leftmost bit for sign)
int b = 7: //00000000000000000000000000000111 ->32 bit signed(leftmost bit for sign)
int result = ~a;//11111111111111111111111111111101 (negative since the leftmost is 1)
System.out.println("~a = " + result ); // prints negative 3
*The result is the 2's complement of three that is negative 3.
result = ~b;//11111111111111111111111111111000 (negative since the leftmost is 1)
System.out.println("~b = " + result ); // prints negative 8
*The result is the 2's complement of eight that is negative 8.

2's Complement Form:The computers use 2's complement form for the negative values so that the mathematical operations made easily like adding 2 + (-2) instead of subtracting.As an example lets obtain the 2's complement for 3 to get the form of -3 used in computer systems.

1.) 3 = 00000000000000000000000000000011
2.) change 1->0 and 0->1 and get 11111111111111111111111111111100
3.) add 1 and get 11111111111111111111111111111101

As you can see, the result in the step 3 is the representation of -3 which we printed out in the previous example as the result of ~a.

Bitshift Operators:

>>(Bitwise signed right shift)  and <<(Bitwise signed left shift operators: Used to shift a bit pattern to left or right by the given number. If the number is negative the signed shift operator will complete the number with 1 instead 0.

Example:
int a = 2; //00000000000000000000000000000010 ->32 bit signed(leftmost bit for sign)
int b = 7: //00000000000000000000000000000111 ->32 bit signed(leftmost bit for sign)
int c = -7: //11111111111111111111111111111001 ->32 bit signed(leftmost bit for sign)
int result = a << 2;//00000000000000000000000000001000
System.out.println("a << 2 = " + result ); // prints 8
result = b >> 2;//00000000000000000000000000000001
System.out.println("b >> 2 = " + result ); // prints 1
result = c >> 2;//11111111111111111111111111111110

System.out.println("c >> 2 = " + result ); // prints -2

>>>(Bitwise unsigned right shift) operator: Used to shift zero into the leftmost position by the given number even if the number is negative. (unlike signed shift operator)

Example:
int a = 2; //00000000000000000000000000000010 ->32 bit signed(leftmost bit for sign)
int b = 7: //00000000000000000000000000000111 ->32 bit signed(leftmost bit for sign)
int c = -7: //11111111111111111111111111111001 ->32 bit signed(leftmost bit for sign)
int result = a >>> 1;//00000000000000000000000000000001
System.out.println("a >>> 1 = " + result ); // prints 1
result = b >>> 1;//00000000000000000000000000000011
System.out.println("b >>> 1 = " + result ); // prints 3
result = c >>> 2;//00111111111111111111111111111110 (in 2s complement form)

System.out.println("c >>> 2 = " + result ); // prints 1073741822

An Example:

Say we want to count the number of ones in a number. To do that we need to find a way to correctly extract the information from each bit of a number. We can accomplish this by using a mask. For example if you mask each bit with 1 using bitwise and operator, only the bits with value 1 will return 1 and other will return zero. We can use the below method which compare the least significant bit in number by 1 and shift the number to right by 1. Note that this is an unsigned shift.

int getNumberOfOnesInNumber(int num) {
int countOnes = 0;
while (num !=  0) {
if ((num & 1 ) == 1) { //compare the LSB with 1
countOnes ++;
}
num = num>>> 1;
}
return countOnes ;
}

Real World Examples:

1.) Compare roles:

When you need masking you can use & operator like below.

Example: suppose in a byte the right most bit shows whether it's an admin or not.
byte admin = 1;//00000001
byte role1 = 1;//00000001 --> admin
byte role2 = 2;//00000010 --> not admin
byte role3 = 3;//00000011  --> admin
byte readRoleFromDatabase = 3;//say we read 3 from the database for the user's role.
If you need to understand whether a user in the database is in role admin you can use "bitwise and";
if ((readRoleFromDatabase  & admin ) > 0) {
}

2.) Check Odd or Even:

You can use "bitwise and" operator to understand a value is odd or even like below.

Example:
byte oddVal = 3;//00000011
byte evenVal = 6;//00000110
byte compareVal = 1;//00000001
if ((oddVal compareVal) > 0)  {
System.out.println(oddVal  + "is and odd value");
}
if ((evenVal compareVal) == 0)  {
System.out.println(evenVal  + " is an even value");
}

Note: In the real world examples we use byte variables and shows 8 bits for them in the command lines.However in the real world the computers use 4 bytes even for the byte variables because of the memory model of the 32 bits computers.If we really want to use 1 byte for a byte variable we have to use a byte array.Since the byte array itself uses 4 bytes for its memory representation, the byte values in it need not has to have 4 bytes instead they has 1 byte for each variable.

Conclusion:

I tried to summarize the usage of the Bitwise and Bitshift operators in Java.As I said, they can be used for masking or in encyription algorithms and they have limited usage in Java.You may use "bitwise and" operator for comparing values or checking if it is odd or even, and may use bitshift operators mostly in encyription or hashing algorithms.

## Sunday, November 2, 2014

### Using Coherence Cache In SOA

Introduction:

Today we'll talk about caching capability of Oracle SOA Suite environment using Coherence Cache adapter.In general, cache coherence is a way of being sure about the consistency of the cached data in a distributed cache environment especially in multiprocessor systems.Oracle Coherence makes the data available as if it were a single application server in a clustered environment and serves the data from the memory if it is available in the cache, instead of a database call.By this way it reduces load on database and reduces the overall response time.In addition to the read-only data where we can use the query the data from the coherence cache, it can also be used for the insert, update and delete operations  lefting the database operation at a later time where the system is not heavy loaded.By this way, even it can be used as a failover scenario if there is a problem with the database.

Configure Coherence Adapter In the Application Console:

First of all, we have to target Coherence Adapter to our server which is the DefaultServer in our case.To do that, go to the http://localhost:7101/console and choose CoherenceAdapter in the Deployments menu.See in Figure 1.

Figure 1

Then, go to the Targets tab and check the DefaultServer as in Figure 2.

Figure 2

Create a Database Connection:

Go to Window -> Database -> Databases and create a new Database Connection like in Figure 3.

Figure 3

Then, create a new table named PERSON with the columns as shown in Figure 4.

Figure 4

Run the following insert queries in the Tools -> Database -> SQL Worksheet screen.

Figure 5

Create the Bpel Process:

Create a synchron Bpel process in the composite page and change the xsd file as in the Figure 6.

Figure 6

Then, drag and drop a Coherence Adapter to be used with Get operation from the Components -> Technology to the composite editor and rename it like in the following figure.

Figure 7

Choose eis/Coherence/Local as the JNDI Name.

Figure 8

Choose the Get operation.See in Figure 9.

Figure 9

On the next screen of the wizard, enter the cache name as "adapter-local" which is a prebuilt internal coherence cache.

Figure 10

Leave the Key field as empty and ignore the following message, we'll enter the Key value later on.

Figure 11

On the next screen, choose the processResponse schema element of the  xsd we have defined earlier as in Figure 12.

Figure 12

The composite now should look like the following figure.

Figure 13

Next, drag and drop another Coherence Adapter to be used with Put operation and rename it.

Figure 14

Choose the Put operation.

Figure 15

Enter again the builtin coherence cache named "adapter-local", uncheck the Auto-generate key, and enter a temporary random key definition which will be overrided soon.See in Figure 16.

Figure 16

Similar to the previous Coherence adapter, choose the processResponse Schema element of the previously defined xsd file.

Figure 17

Before go in to the detail of the Bpel process, the final step is to define the database adapter, so drag and drop a database adapter to the composite editor.

Figure 18

Choose the connection we've just created and enter the builtin JNDI eis/DB/SOA like in Figure 19.

Figure 19

Choose only the Select operation on the following screen.

Figure 20

Import the table PERSON we've created previously.

Figure 21

Check all the columns of the table PERSON like in Figure 22.

Figure 22

Now, define the parameters and the SQL.We want to select a person with a id.See in Figure 23.

Figure 23

At the end we should get the following diagram in the composite page.

Figure 24

Build the Detail of Bpel Process in the Bpel Editor:

Go into the bpel editor and drag and drop an invoke activity after the receiveInput component.Reference the Coherence Adapter with Get operation from the invoke activity.Then define the input and output variables by just clicking the green plus sign like in Figure 25.

Figure 25

The bpel process should look like the following now.

Figure 26

Go to properties page of the invoke activity and click to the green plus sing to add a new property.

Figure 27

In the opened "To Propery" page choose the jca.coherence.Key property and give its value as the id of the input variable as in Figure 28.

Figure 28

Then, drag and drop an If activity just after the Coherence Adapter with the Get operation and define the following condition to understand whether the data is in the cache.If the name property of the output of the Coherence adapter will not be null that means the data is in cache.

Figure 29

If its returned true from the If activity just assing the output of the Coherence Get Adapter to the output variables by dragging and dropping an assing activity.See in Figure 30.

Figure 30

If its returned false from the If activity, in the else part drag and drop an invoke activity and reference it to the Database Adapter we've defined choosing the input and output variables by just clicking the green plus sign as we did previously, and assing the input id to the input variable of the database adapter with the help of an assign activity. See in Figure 31 and 32.

Figure 31

Figure 32

Next, drag and drop an invoke activity referencing to the Coherence Adapter with the Put operation.See in Figure 33.
Go to properties page of the invoke activity and click to the green plus sing to add a new property and define the jca.coherence.Key property as the input id as in Figure 34.
Then, drag and drop another assing activity and assign the output of the database adapter that just select the person information to the input of the Coherence Adapter with the Put operation and at the same time to the output variable of the bpel process to reply to the caller.See in Figure 35.

Figure 33

Figure 34

Figure 35

The final bpel process should now look  the following figure.

Figure 36

Test the Bpel Process In the Enterprice Manager:

Go to the http://localhost:7101/em, open your project, click on Test and call the service using the same values that exists in the database for example with the id 1.

Figure 37

You'll see in the first execution there will be database call, while the later calls will get the data from the cache coherence.You can see this execution paths in the "Flow Instances" page of the enterprice manager as in the Figure 38 and 39.

Figure 38

Figure 39

Conclusion;

We saw how to use a coherence cache in Oracle SOA Suite environment.The coherence cache can greatly improve performance of an application by serving the data from the memory rather than requesting the database all the time and by reducing the load on database.

You can download the source code from here