LppEdd
LppEdd

Reputation: 21154

Bit shifting in RPGLE

I languages such as Java or C, we got to use bit shifting operands. Example:

myValue <<= 1

How can I accomplish the same thing in RPGLE? (if possible)

Upvotes: 0

Views: 848

Answers (3)

jmarkmurphy
jmarkmurphy

Reputation: 11483

RPG is not a bit twiddly language. It was created to solve business problems, and bit shifting is not typically something one needs to solve a business problem. However, RPG IV is an ILE language that plays nicely with other ILE languages. If you need to shift bits, you could create a function in C that performs the shift, and call it from your RPG program.

So if you have a C function called ishiftl that shifts an integer left:

int ishiftl(int i) {
    return i << 1;
}

You need to create an RPG prototype like this:

dcl-proc ishiftl int(10) ExtProc('ishiftl');
  i       Int(10) value;
end-proc;

Then you can call it like this:

dcl-s result       Int(10);
dcl-s num          Int(10);

result = ishiftl(num);

Note: C is a case sensitive language and the case in which you define the function name is significant. RPG on the other hand is not case sensitive, and all identifiers are implicitly converted to upper case. So ExtProc('name') is required to explicitly define the name of the C function in the case it was defined. Also, there are some things to remember. One is C passes arguments by value. If you are not particularly familiar with how to prototype C functions in RPG, Barbara Morris wrote a cheat sheet to help. It is located here.

Upvotes: 1

Charles
Charles

Reputation: 23803

You can't.

AND , OR, XOR, yes...shift no.

You seem to think everything you can do in another language should be doable in RPG. It's not...and that's ok. The great thing about ILE is you could easily create a C procedure that does bit shifting and call it from RPG if you need it.

You could of course multiply/divide by 2^n....same results, and perhaps the compiler optimizes it to a bit shift. But you'd have to be working with an unsigned integer variable.

Upvotes: 2

Barbara Morris
Barbara Morris

Reputation: 3674

For <<, divide by 2, 4, 8 etc. For >>, multiply by 2, 4, 8 etc.

To get the 2, 4, 8 etc from n = 1, 2, 3, use %inth(2 ** n).

For division, use %div rather than the / operator, to avoid getting decimal places.

C: 
   n = 5;
   i <<= n;
   i >>= n;

RPG:
   n = 5;
   i *= %inth(2 ** n);
   i = %div(i : %inth(2 ** n));
 or
   i = shift_left (i : n);
   i = shift_right (i : n);

dcl-proc shift_left;                             
   dcl-pi *n int(20);                            
      value int(20) value;                       
      shift int(20) value;                       
   end-pi;                                       
   return value * %inth(2 ** shift);             
end-proc;                                        
dcl-proc shift_right;                            
   dcl-pi *n int(20);                            
      value int(20) value;                       
      shift int(20) value;                       
   end-pi;                                       
   return %div(value : %inth(2 ** shift));       
end-proc;                                        

Upvotes: 3

Related Questions