Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Bitwise AND isn't working for me...

Former Member
0 Kudos

Hi,

I'm trying to do something that would be extremely easy for me in a language like C or C++, but since I know nothing about SAP or ABAP except what I've read in the past few days, this problem is a little harder than I had hoped.

I have some ABAP code written by someone else long ago, which I need to fix. Here is the part of the code that I'm trying to modify:


IF usr02-uflag = 0.
      zreturn-number = '245'.
      zreturn-message = 'USER IS IN ENABLE STATE'.
      APPEND zreturn TO return.
      CLEAR zreturn.

*"----------------------------------------------------------------------
* value '64' is stored, if input user is in lock state done by
* adminstrator
*"----------------------------------------------------------------------
    ELSEIF <b>usr02-uflag = '64'.</b>
      zreturn-number = '777'.
      zreturn-message = 'USER IS LOCKED BY ADMINISTRATOR'.
      APPEND zreturn TO return.
      CLEAR zreturn.

I want to change the comparison <b>usr02-uflag = '64'.</b> so that it AND's the value with 64 so I can see if that particular bit flag is turned on.

I haven't been able to find any type declaration for usr02 yet, but I'm assuming usr02-uflag is some kind of string or xstring...?

I tried converting the string to a hex number and using the BIT-AND operator, but I didn't get the correct value when I did that.

Can someone please show me the quickest/easiest way to do the following (pseudo-code of course):

ELSEIF ( usr02-uflag BIT-AND 64 ) = 64.

15 REPLIES 15

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

It is an integar field. What exactly is the problem?

Regards,

RIch Heilman

0 Kudos

Well as I said, I don't know what the data type of usr02-uflag is, but since it is compared with a number in quotes, I'm assuming that means it's a string?

usr02-uflag holds a SAP user's Locked status. 64 means they were locked by the administrator and 128 means they were locked by themselves when entering the wrong password a few times. I've noticed that it is possible for a user to be in both states at the same time. Since that would produce a value of '192' neither of the ELSEIF conditions would catch it and it would fall through to the default.

I could explicitely test for a value of '192', but since I don't know what other bits might be turned on for other user status's, I'd rather AND the value with '64' or '128' to determine if the user is Admin or User Locked...

Here is some more code at the top of the ABAP. I'm not sure if it helps much?


*"----------------------------------------------------------------------
* tables used for fetching the status of an user
  TABLES: usr02.
*"----------------------------------------------------------------------
*"----------------------------------------------------------------------
  DATA : BEGIN OF zreturn .
          INCLUDE STRUCTURE bapiret2.
  DATA : END OF zreturn.
*"----------------------------------------------------------------------
* Fetch record from system table USR02. This table is used to store User
* data

  TRANSLATE username TO UPPER CASE.

  SELECT SINGLE * FROM usr02 WHERE bname = username.
*"----------------------------------------------------------------------

Former Member
0 Kudos

the field ufla is an int...so try just

ELSEIF usr02-uflag = 64. (without ' ' )

regards, Sebastián.

Former Member
0 Kudos

usr02-uflag is an integer .

if elseif doesn't works, you should try with case

case usr02-uflag.

when 0.

zreturn-number = '245'.

zreturn-message = 'USER IS IN ENABLE STATE'.

APPEND zreturn TO return.

CLEAR zreturn.

when 64.

zreturn-number = '777'.

zreturn-message = 'USER IS LOCKED BY ADMINISTRATOR'.

APPEND zreturn TO return.

CLEAR zreturn.

when .........

endcase.

I hope this help.

0 Kudos

Thanks,

That might make the ABAP a little more efficient, but it doesn't solve the ANDing problem.

If usr02-uflag really is an int, then how do I AND two int's together?

I've tried using <b>ELSEIF ( usr02-uflag BIT-AND 64 ) = 64.</b> but when I check the syntax, it returns "Relational operator "BIT-AND" is not supported"

Former Member
0 Kudos

don't use the bit-and operator,

just write this:

ELSEIF  usr02-uflag = 64.

Regards, Sebastián.

0 Kudos

That is essentially what the code is currently set to.

The bug that I'm trying to fix in the code is when a user is both Admin Locked and User Locked (i.e. uflag set to 64 & 128 == 192). In that case, the current code doesn't catch it because uflag is not equal to 64 or 128.

Since I can't find any documentation on the usr02 table, I don't know what else uflag might be used for (i.e. maybe expired password or something like that). So if I AND the values, it should fix this bug no matter what value is stored in usr02-uflag.

If uflag = 192, and then I AND that value with 64, I will end up with 64.


    11000000   <-- 192
AND 01000000   <-- 64
------------
    01000000   <-- 64

0 Kudos

Still not exactly sure what you are trying to get at, but maybe this is what you want?

    ELSEIF ( usr02-uflag = '64'
              or usr02-uflag = '128'
              or usr02-uflag = '192' ).
      zreturn-number = '777'.
      zreturn-message = 'USER IS LOCKED BY ADMINISTRATOR'.
      APPEND zreturn TO return.
      CLEAR zreturn.

Regards,

Rich Heilman

0 Kudos

I don't think I can make it much clearer than that.

Using your example of comparing for all 3 values; what happens if usr02-uflag is set to 193, 194 or 255...? All of those numbers have the 7th and 8th bits turned on, but the ELSEIF statement wouldn't get called for any of those values.

Basically, I just want to know if the 7th and/or 8th bit in the integer is set to 1. All other bits can be set to 0 or 1, I don't need to know what their value are:

????????11??????

Another term for what I'm trying to do is called Bit-Masking. For example:


0 AND 0 = 0     0 OR 0 = 0
0 AND 1 = 0     0 OR 1 = 1
1 AND 0 = 0     1 OR 0 = 1
1 AND 1 = 1     1 OR 1 = 1

0 Kudos

Wow man, I am really confused now. The field can hold integar values, thats it. Here are the values that it can contain at any given time according to the domain of the field. If you look at the domain XUUFLAG in SE11, click on the value range tab, you will see the following.

0 Not Locked

32 Locked Globally By Administrator

64 Locked Locally By Administrator

128 Locked Due To Incorrect Logons (Limited Term)

These are the values that this field can have. Simply check that it is <> 0. Should solve your problem.

ELSEIF usr02-uflag <> 0

I am really at a lost when it comes to what you are referring to in regard to the 7th and 8th bit.

Regards,

RIch Heilman

0 Kudos

Sorry, I'm coming from a C/C++ background where working with individual bits is a common practice.

I think you may have given me 1 more value to check though. (I never knew about the SE11 screen or XUUFLAG, since I've only been using SAP for a few weeks now)

I have 2 questions based on the values listed there.

First, what is the difference between Locked Globally and Locked Locally? Is 32 a new value since SAP 4.5 (when this ABAP was originally written)?

Second, is there any guarantee that SAP won't add additional values to XUUFLAG in the future?

0 Kudos

Honestly, I have no idea what the difference is between locked locally and locked globally is. It may have something to do with multiple application servers, not sure.

No idea about the values either, when ones were added, etc.

SAP could very well create a new one at any release. But for your purposes it doesn't really matter, I think you really only want to know that the user has been locked, so simply checking that the field value is <> 0 would cover the bases.

Regards,

RIch Heilman

0 Kudos

Actually, I need to distinguish between an Admin Lock and a User Lock because my company's program allows the user to reset their own password if they forgot it (after asking them several questions to verify that they are who they claim to be). When we reset their password, we also unlock the user; but if the user was locked by the Admin, we don't want to unlock the user.

So this is why I was wondering if SAP might add additional Lock states in the future (maybe something like "Locked Due To Incorrect Logons Globally" or whatever).

I guess I'll just have to use a CASE statement and cover the 9 different conditions that are possible with the current SAP version. i.e. 32, 64, 128, and every combination of those 3, like 32 + 64, 64 + 128...

Thanks for the help.

0 Kudos

Have you actually seen the values added together in this field in the database, transaction SE16? I have never seen this kind of behavior. As I see it, this field can only have the four mentioned values.

Regards,

Rich Heilman

0 Kudos

I never even knew about SE16 either. Thanks.

When I locked the user by entering the wrong password 5 times, then did an Admin lock (you have to do it in that order, not the other way around) I looked in SE16 and I did indeed see 192 listed for the UFLAG.

Since the language doesn't seem to be very user friendly when it comes to bit-masking, I had to use the following CASE statement:


CASE usr02-uflag.
    WHEN 0.
        "User is not locked...
    WHEN 32 OR 64 OR 96 OR 160 OR 192 OR 224.
        "User is Admin Locked...
    WHEN 128.
        "User is Locked by User...
    WHEN OTHER.
        "Unknown condition...
ENDCASE.