|
No, that's the job of the AND operation. You're getting very close. X'10' does indeed equal B'0001 0000'. In hexadecimal, each hex digit represents four binary digits (in fact, that's what bit means: Binary digIT). Okay, now you need to take a look at the AND operation. There are two operands and the result, right? Okay, the first operand is the original value, and the second operand is the "mask". Basically, the AND will turn off every bit in the original value if the corresponding bit in the mask is zero. So: B'0111 0000' AND B'0001 0000' = B'0001 0000' Another way to look at this is: B'0111 0000' B'0001 0000' ------------ B'0001 0000' See how bits one and two get turned off, but bit three stays on? That's because with an AND operation, in order for the bit to be on in the result, the bit must be on in both the original value AND the mask (thus the name of the operation). So, let's look at a couple more examples: B'0001 0000' AND B'0001 0000' = B'0001 0000' B'0001 1111' AND B'0001 0000' = B'0001 0000' B'0101 0101' AND B'0001 0000' = B'0001 0000' B'1111 1111' AND B'0001 0000' = B'0001 0000' Get the idea? Basically, the AND strips out anything except where you have bits on in the mask. So, in this particular case, as long as bit one is on, the result will be B'0001 0000'. Similarly, anything with bit one off will end up as B'0000 0000': B'0000 0000' AND B'0001 0000' = B'0000 0000' B'0000 1111' AND B'0001 0000' = B'0000 0000' B'0000 0101' AND B'0001 0000' = B'0000 0000' B'1110 1111' AND B'0001 0000' = B'0000 0000' Because there is only one bit on in your mask, there are only two possible results: if bit three is on, the result is B'0001 0000', if bit three is off, the result is B'0000 0000'. So, the way to check if bit three is on is to AND it with a mask of B'0001 0000' and then see if the result is also B'0001 0000'. Since B'0001 0000' is equivalent to X'10', you get the following: bit3ison = (%BitAnd(YourField:X'10') = X'10'); In general, to test that one or more bits are all on, create a mask with those bits on. Then, AND the original value with the mask and make sure the result is the same as the mask. (If ALL the bits are off, the result will be zero; if ANY of the bits are on the result will be non-zero.) Finally, typically, I'd use a named constant for this. For example: d BIT_THREE c x'10' d BITS_0123 c x'F0' Then I could say: bit3ison = (%BitAnd(YourField:BIT_THREE) = BIT_THREE); nohighbits = (%BitAnd(YourField:BITS_0123) = 0); Hope this helps! Joe > From: Fisher, Don > > A little. I guess I'm a little slow when it comes to this stuff. I don't > understand why X'10' is being used both as the comparison value and the > identification of the bit, unless X'10' is the hexadecimal equivalent of > '0001 0000'. > > If that's the case, I either need to figure out all the possible > hexadecimal > values where the third bit (bit number 2) can be on or a method of > determining the status of that third bit directly. > Sent: Tuesday, May 31, 2005 7:17 AM > To: 'RPG programming on the AS400 / iSeries' > Subject: RE: Bit Operations
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.