Extracting a bit can be done a number of ways, but the code in this article uses only the basic arithmetic capabilities of CL.
This article is sort of the antithesis of what we normally do in RPG Developer. Sometimes, you need to do things in CL without calling an RPG program. The situation is rare, but it happens. One case may be where you have to provide the source to the program in question but you can't count on the RPG compiler being there. And no matter how much IBM has improved the language (and it's been quite a bit), some things really can't be done in CL. But in this article, I want to show you how to do something in CL you may have thought was not possible: extract the value of a single bit.
What’s the Problem and Its Solution?
The problem definition is simple: given a character and a bit position, return either true or false. For example, if I pass in a blank (x'40') and the bit position 0 (zero), then I would expect the proposed function would return false, since the leftmost bit is off. Sticking with the traditional zero-based bit position, if I changed the index to 1, I would expect to see true as the result. All other index values would return false.
Simple enough? The problem, though, is that CL doesn't have any bit manipulation opcodes. The only way to check a bit is the way we did it in the old days: shift and check the bit. If you're not as old as dirt, you might enjoy a little programming archeology: here's a discussion on shift and carry instructions. It's a little more complicated these days because we don't have quite as low-level instructions in CL, but being good programmers we can make do. Below is a program that will do the trick.
BINARY: PGM (&CHAR &POS &STATE)
DCL &CHAR *CHAR 1
DCL &POS *DEC (1 0)
DCL &STATE *LGL
DCL &CHAR2 *CHAR 2
DCL &BINARY *DEC (5 0)
DCL &BINARY2 *DEC (5 0)
DCL &BIT *DEC (1 0)
DCL &INDEX *INT
DCL &MSG *CHAR 9 VALUE('RESULT:')
/* Convert character to binary */
CHGVAR &CHAR2 (X'0000')
CHGVAR %SST(&CHAR2 2 1) &CHAR
CHGVAR &BINARY %BINARY(&CHAR2)
/* SHIFTR if necessary (any position but last) */
DOFOR &INDEX FROM(1) TO(7 - &POS)
CHGVAR &BINARY (&BINARY / 2)
/* Now get last bit (find remainder of divide by 2) */
CHGVAR &BINARY2 (&BINARY / 2)
CHGVAR &BIT (&BINARY - (&BINARY2 * 2))
CHGVAR &STATE (&BIT *EQ 1)
/* Debug: Display the result */
/* CHGVAR %SST(&MSG 9 1) &BIT */
/* SNDPGMMSG MSG(&MSG) TOPGMQ(*EXT) */
The program will test the specified bit of the input value and return either on or off, depending on the state of the bit. It's very simple, but it does take a little bit (no pun intended) of code, so let me run through it.
First are the parameters. The program gets three parameters: the single character value and the single digit zero-based bit position to test are input values while the logical state is the output variable. These are &CHAR, &POS, and &STATE, respectively. Next, I define some work variables. You'll meet up with them later. I also define an index, which is used in a FOR loop. You'll note that I use *DEC for all the work variables, but *INT for the index. That's because I am counting on the arithmetic being integer arithmetic that will not half-adjust. I didn't realize it until I did some testing, but arithmetic with variable of type *INT automatically half-adjusts, so my work values need to be *DEC. On the other hand, the DOFOR instruction requires a variable of type *INT, thus the definition of &INDEX. Finally, I created a work variable named &MSG, which is only used if the debugging lines at the end of the program are uncommented.
OK, the first thing I have to do is to convert my single character to a binary number. That's relatively easy using the %BINARY built-in, with the only issue being that %BINARY requires a variable of either two or four bytes. So, to widen my value, I use &CHAR2, which is two characters in length, and set it to null. I stuff the input value into the second byte of &CHAR2 and then convert that to my &BINARY field (which is really a decimal field, but you get the idea).
The next step is to shift the value right if needed. Take a look at this diagram of the shift instruction. The idea is to get the bit we want to test to be in the rightmost position of the &BINARY variable. If the position we want to check is 7, then no shifting is needed! (Remember, we're using a zero-based position, so the leftmost bit is bit 0, while the rightmost bit is bit 7.) For any other position, we need to divide the original number by the appropriate power of two. If we want the bit in position 6, we have to divide by 2. If we want bit 5, we have to divide by 2 twice. The loop in this program will execute once if the original position is 6, twice if it is 5, and so on. It will not execute at all if the specified position is 7, which is exactly what we want because the desired bit is already in the last position of the variable. (Extra credit: Why won't the loop execute for position 7?)
Now that we have the correct bit in the rightmost position, the last thing is to isolate that bit. We do it by first dividing by two once again and then subtracting twice that result from the shifted value. Follow this through: if the value in &BINARY is 3, then the last bit is on. OK, divide 3 by 2, get a result of 1. Then, subtract (1 * 2) from 3, and end up with a result of 1. This is good! What if the value is 4? Divide 4 by 2, get 2. Subtract (2 * 2) from 4; get 0! Perfect! So our integer arithmetic allows us to get the value of the last bit, either 0 or 1, into the variable &BIT. The last thing is to convert that to a logical. That's done by setting the &STATE variable to the result of comparing &BIT to 1.
That's all the program needs. It should now work as designed. However, in case you want to double-check your results, you can uncomment the next couple of lines. This will set the &MSG variable with the contents of &BIT and show you the result. Now, you can do this in RPG quite easily and there's also an MI instruction that can be invoked from ILE CL programs. But hopefully this brief example shows you a couple of tricks that you can use in other CL programs. Enjoy!