SCJP prep help -- bitwise bad boys?

Hi,
I'm studying for SCJP so obviously one of the details I have to brush up on are the oft-unused bitwise operators: & | ^ ~
I'm wondering if there are any mental techniques to using these in your head. Instead of literally writing out the number in binary, performing the comparison, and then re-converting back to decimal.
Similar to how in grade school we learn that you can take a shortcut of multiplying two long numbers:
34563
x 24
and do these steps:
3 * 4 = 12, carry the 1
6 * 4 = 24, add the 1 is 25, carry the 2
5 * 4 = 20, add the 2 is 22, carry the 2
4 * 4 = 16, add the 2 is 18, carry the 1
3 * 4 = 12, add the 1, it all equals 138252
next line
then pad a zero on the right
3 * 2 = 6
6 * 2 = 12, carry the 1
5 * 2 = 10, add the 1 is 11, carry the 1
4 * 2 = 8, add the 1 is 9
3 * 2 = 6, it all equals 691260
altogether the answer is 691260 + 138252 = 829512
In other words, while it's not feasible to add 34563 to itself 24 times (23 actually), there is a trick to figuring it out. Is there something like that for bitwise operators?
Thanks.

Thanks -- I finally collated some good tips a few days after I posted that here, with some help from local LUGgers. I'll post them here for the archives:
Would you mind posting a summary of the helpful tips, tricks, and
shortcuts sent your way? I'm rather interested in understanding this
stuff a little better myself.Sure. In fact, they say you really know something well when you can
explain it to someone else, and this gives anyone a chance to correct me
if I'm wrong in what I think I know. Here's some of the things I
learned, combined with what I had already learned from my book (please
do correct me if I'm wrong):
1. The unary bitwise NOT operator (~) has a shortcut, but one which
only works when two's complement is in effect. On systems in which
one's complement is in effect, it doesn't work. The shortcut is that
any number n has a bitwise NOT of -n - 1. So
~5 == -6
~-12 == 11
Because I'm fortunate enough to be working in Java where you're writing
for only one platform (the JVM, which has a spec), I don't have to worry
about exceptions to this for the test.
2. Other than the NOT operator, the bitwise operators are binary
operators, working on pairs of numbers. The bitwise AND operator
returns a 1 for each pair of bits that are both 1.
So in the case of
11001 & 10011
the result is 10001 (because only the first and last digits are both 1).
The bitwise OR (|) operator returns a 1 for situations in which either
pair of bits has a 1.
So in the case of
11001 | 10011
the result is 11011 (because the only place in both numbers where there
is no 1 is the third digit).
The bitwise XOR (^) operator returns a 1 for situations in which either
pair of bits, but not both, has a 1 (hence the name "exclusive OR", or XOR).
So in the case of
11001 ^ 10011
the result is 01010 (because in the first, third, and fifth digits,
either both numbers are 0 or 1).
The short circuit operators AND (&&) and OR (||) work just like their
regular counterparts except they stop evaluating once they "know" the
result (AND stops evaluating if the first operand is false, and OR stops
evaluating if the first operand is true).
3. So my original question, "does anyone know any mental shortcuts for
working with bitwise operators", should have been phrased as "does
anyone know any mental shortcuts for converting decimal numbers to
binary". The answer, which a couple of people provided, was that it's
much easier to convert to binary (even if only in your head) when you
are working from octal or hexadecimal, and that it's not too difficult
to convert from decimal to octal or hexadecimal.
Several ways were suggested:
a) Convert the number to hex, then do the comparison a byte at a time in
your head using a table:
& 0x01 0x02 ... 0xff
0x01 0x01 0x00 0x01
0x02 0x00 0x02 0x02
0xff 0x01 0x02 0xff
b) Convert the number to octal using a simple recursive divide-by-eight
solution:
divide the number by eight, store the remainder as the low bit depth
(rightmost number), then repeat the process with the quotient.
123 / 8 = 15 remainder 3 -- 3 is rightmost digit (3)
15 / 8 = 1 remainder 7 -- 7 is next to 3 (73)
we don't divide 1 by 8 -- 1 is next to 7 (173)
so 123 in octal is 0173
From octal it's much easier to see the binary form of the number:
1 7 3
001 111 011
And from binary it's much easier to do a bitwise calculation.
c) Another way to convert to binary is to simply keep dividing by two
and assigning 1 if there's a remainder and 0 if there isn't, then read
in reverse to get the binary number:
<quote>
If a number is even, dividing it by 2 will have 0 remainder.
If it is odd, dividing it by 2 will have a 1 remainder. Dividing
by 2 is so easy that I, at least, can just write down a column of
successive halvings, along with 1s and 0s for their oddness.
459 1
229 1
114 0
57 1
28 0
14 0
7 1
3 1
1 1
Reading from bottom to top, we again get 111001011. In this
presentation you stop when the quotient is 1, since you've already
written down the remainder that you'd get by dividing 1 by 2 when
you write down the oddness. Remainder when dividing by 2 is easier
than quotient.
</quote>
d) A final useful tip mentioned: as long as x is not zero,
x & -x
will always evaluate to a power of two, which means only one bit is set
in the number (the least significant of the bits of x that were 1s).
4. And everyone agreed that it's foolish to do these kinds of
computations in your head when there's no good reason not to use a
computer to do it. My personal preference is to use Python since the
interactive interpreter works just like a calculator, though any
scripting language should probably be able to handle the calculations.
(Here's a quick sample:)
|$ python
|Python 2.2.2 (#1, Dec 31 2002, 12:24:34)
|[GCC 3.2 20020927 (prerelease)] on cygwin
|Type "help", "copyright", "credits" or "license" for more information.
|>>> 12 & 14
|12
|>>> 12 | 14
|14
|>>> 12 ^ 14
|2
|>>> ~12
|-13
|>>> myResult = 0x5422CA66 & 458
|>>> myResult
|66
|>>> hex(myResult)
|'0x42'
(hey, it wouldn't be a LUG without a little evangelism, would it? :)
Much thanks to Bill, Jason, Kevin, and YATArchivist.
Erik

Similar Messages

  • I purchased bad boys 1 and 2 from the itunes store and they would not play so i restarted my laptop and now they are not on my itunes

    i purchased bad boys 1 and 2 from the i tunes store but they would not play so i restarted my laptop and now they are not even on my itunes

    Try:
    - Reset the iPod. Nothing will be lost
    Reset iPod touch: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Delete the songs and redownload:
    Downloading past purchases from the App Store, iBookstore, and iTunes Store

  • I need help really bad. im trying to sync music on my ipod, but a message comes up say " cannot synce  photoes in ipod because the folder pictures cannot  be found . what do i do ?

    i need help really bad. im trying to sync music on my ipod, but a message comes up say " cannot synce  photoes in ipod because the folder pictures cannot  be found . what do i do ?

    Try deleting the iPod Photos Cache folder. There may be more than one. For its location see:
    iTunes: Photo sync creates iPod Photo Cache folder
    Also, go to the Photos pane for the iPod in iTunes and make sure syncing photos is unchecked.

  • HT203175 I cannot remove  iTunes Helper .exe- Bad Image warning from my PC when it opens. Can someone help a luddite?

    I receive an error message each time I start my PC. I get the I Tunes Helper.exe- Bad Image message and need to click 5 times before it goes away. I was trying to sync my I Phone with OUtlook and something happened. I am not tech-savvy.

    Many thanks.
    Taken at face value, you're having trouble with an Apple Mobile Device Support file there.
    Let's try something relatively simple first.
    Restart the PC. After you've fought your way through the error messages, go into your Uninstall a program control panel, select "Apple Mobile Device Support", and click "Repair".
    Does the repair install seem to go through okay? If so, restart the PC again. Have the error messages from iTunesHelper.exe stopped?

  • Why can't I gift Bad Boys Movie Bundle?

    I Want to gift the Bad Boys movie bundle but the Gift Option isn't there.

    after some more testing: i can gift every other app. previously purchased or not. but NOT "cover orange" …

  • Urgent help in BADI

    Hi Abappers,
        Kindly help me out as i am in trouble. I have got a requirement to add customized fields onto O4_TIGER screen using BADI. I have got the name of the BADI definition ie., OII_DIP_ADD_COLUMNS . This screen is related to MM -> Inventory mangement -> Is-OIL.
    Can anyone pl help me out of how to add fields . Coz i have absolutely nil knowledge in BADIs and Userexits. I would be very much pleased if some one suggest me a step by step process.
    Regards.

    <b>first u define BADI in " SE18", and implement in "SE19".
    for predefined GLOBAL CLASS : CL_EXITHANDLER which can enable BADI service.
    method-->GET_INSTANCE (which can create instance of a class)
    gowith SE18 --> NAME..... Z***** --> CREATE,
    click check box MULTIPLE USE.
    select INTERFACE tab
    INTERFACE ..... ZIF_EX_****   <-----click on  this -
    > enter.
    save details.
    METHODS                  LEVEL           DESCRIPTION
    SELECT-DATA          INSTANCE            SELECTING DATA
    select PARAMETERS
    PARAMETER                      TYPE                 TYPING
    CUST_LOW                     IMPORTING            TYPE           KNA1-KUNNR      
    CUST_HIGH                    IMPORTING             TYPE          KNA1-KUNNR
    SAVE DETAILS.
    implementing BADI -
    > SE19
    implementation name  ...... Z**********-----> CREATE
    DESCRIPTION--...DEMO FOR BADI
    select--> INTERFACE
    for method> SELECT_DATA   <--- click on this ---> enter
    save details.
    METHOD
    DATA : ITAB TYPE TABLE OF KNA1,
                WA TYPE KNA1.
    SELECT KUNNR LAND1 NAME1 ORT01 FROM KNA1 INTO TABLE ITAB WHERE KUNNR BETWEEN CUST_LOW AND CUST_HIGH.
    LOOP AT ITAB INTO WA.
    WRITE:/ WA-KUNNR,WA-LAND1,WA-NAME1,WA-ORT01.
    ENDLOOP.
    ENDMETHOD.
    F3 ,       SAVE and ACTIVATE.
    LETUS CREATE EXECUTABLE PROGRAM (SE38).
    PROVIDE TABLE WA.
    REPORT Z*******
    TABLES: KNA1.
    SELECT-OPTIONS: S_KUNNR FOR KNA1-KUNNR.
    declaration.
    CLASS CL_EXITHANDLER DEFINITION LOAD.
    interface reference
    DATA: OBJ_EXIT TYPE REF TO ZIF_EX_*****.
    START-OF-SELECTION.
    gowith PATTERN ---> AABAP object pattern --> ENTER.
    CALL METHOD
    CLASS----   CL_EXITHANDLER
    METHOD---  GET_INSTANCE
    ENTER.
    CALL METHOD.............
    CHANGING
    INSTANCE  =  OBJ_EXIT.
    gowith PATTERN> AABAP OBJECT--> ENTER
    CALL METHOD
    OBJ---> OBJ_EXIT,
    CLASS---> ZIF_EX_*******,
    METHOD----> SELECT_DATA.
    CALL METHOD.................
    EXPORTING
    CUST_LOW  = S_KUNNR-LOW
    CUST_HIGH  = S_KUNNR-HIGH. 
    SAVE and  ACTIVATE.
    EXECUTE.</b>

  • Help in BAdi for MIGO

    hi friends,
    i have a requirement in which, when i post goods in migo, a pop up should come where i have to enter a form(form-31) details and this should get reflected in an ztable along with the vendor details. i have found that a BAdi(MB_DOCUMENT_BADI- i am using SAP R/3 5.0 ) exists with two methods
    1.'MB_DOCUMENT_BEFORE_UPDATE'
    2.'MB_DOCUMENT_UPDATE'.
    which one should i use? becos in the documentation it says that 'MB_DOCUMENT_BEFORE_UPDATE' is called before any accounting documents are generated and if i write any database commit in this method then if some error occurs after the business add-ins are processed we cannot carry out a complete ROLL BACK,as the data up to the COMMIT has already been written in the database(ztable),it will lead to data inconsistency.
    again in 'MB_DOCUMENT_UPDATE' it says is processed after the FI document numbers are called and for For performance reasons, you should not re-read the tables or write to any tables or carry out any time-consuming routines in this method..
    pls advice if there is any other BAdi or any work around...waiting for your replies...and pls be descriptive...
    also if the user cancels or exits the popup then processing should not proceed..it should come back to the migo initial screen without posting the goods...how to go about it?
    regards
    kiran
    pls ...help is urgent...every effort is appreciated
    Message was edited by: kiran k

    Hi
    If you call a popup I don't think you can use the method 'MB_DOCUMENT_UPDATE', as here you can update your Z-TABLE without the COMMIT. So I suppose this method works in background and in this step any popup can be shown.
    So you should use 'MB_DOCUMENT_BEFORE_UPDATE', but I don't know I you can stop the saving, try to use an error message.
    Max

  • Changing the  F4 help in BADIs of XK01

    Hi,
    I ve implemented the BADI defn VENDOR_ADD_DATA for XK01 and the method is PRESET_VALUES_CCODE..
    For some fields there are certain values in the F4 help now I need to change the F4 help to show the values according to my requirement..
    Ex for Cashmanagmnt group in XK01 the F4 help is ... A, B, C, K,J,T,M.(This Values are feteched from Predefined table T035 and I cant delete the Values from that table)
    now if the condition which I given in the coding satisfies I should get the F4 help as.. A, B,T,M. Only
    Please answer to my question...
    please please please...
    Edited by: Nalina Acharya on May 21, 2008 9:59 AM

    Thnx for the answers

  • Required help for badi for GL tab in MIRO transaction

    Hi,
    I am working in MIRO transaction.
    I am having a requirement as below:
    While creating invoices, In the gl tab when we enter gl account number system should populate Tax jurisdiction code by default.
    I am using BADI EXTENSION_US_TAXES method MM_ITEM_TAX_MODIFY but it populates the tax jurisdiction in PO reference tab not in GL tab.
    Please advice
    Regards,
    Suvarna Nandi

    Hi
    Have you tried with enhancement FYTX0002? See also Note 302998 - Collecting fields for user-exit. It's an idea.
    I hope this helps you
    Regards
    Eduardo

  • Help on BADI HRHAP_OFFLINE

    Hi Experts,
    I am having some difficulties with BADI HRHAP_OFFLINE (available in ECC 6.0).
    This BADI is having standard SAP implementation HRHAP_OFFLINE01 in ECC 6.0. I want to attach customized smartform for appraisal form (for upload/download fucntions) using this BAPI.
    I have created and activated Z Implementation of HRHAP_OFFLINE, but cannot find parameter to link smartform in any of the methods of this BAPI.
    Any help will be highly appreciated.
    Thanks in advance.
    Regards,
    Mohaiyuddin.

    solution : Copy and Edit hap_pdf_offline_form and attach it to the implementation of BADI HRHAP_OFFLINE.

  • Calling Search Help in BAdI implementation.

    Hi All,
    I have created my search help with search help exit function. Now I need to call this Search Help in my BAdI Implementation ( Method of BAdI) . Please suggest me which function I should use to call my search help.

    Hi,
    Please use the following FM. It might be helpful.
    F4IF_SELECT_VALUES
    Regards
    Bhupal Reddy

  • Needed Help on BAdi ADDRESS_UPDATES

    Hi All,
    I need to work in ADDRESS_UPDATE BAdi. Could Pleaze any respond and provide some valuable inputs to complete following requirement.
    Validates Zip code and Transportation Zone in LZONE against Zcustom table. If not found Zip code in the Zcustom table. need to raise Error message and stop to save.
    Regards,
    K.Vinai
    Helpul answers will surely rewarded.

    Hi
    DEFINING THE BADI
    1) execute Tcode SE18.
    2) Specify a definition Name : ZBADI_SPFLI
    3) Press create
    4) Choose the attribute tab. Specify short desc for badi.. and specify the type :
    multiple use.
    5) Choose the interface tab
    6) Specify interface name: ZIF_EX_BADI_SPFLI and save.
    7) Dbl clk on interface name to start class builder . specify a method name (name,
    level, desc).
    Method level desc
    Linese;ection instance methos some desc
    8) place the cursor on the method name desc its parameters to define the interface.
    Parameter type refe field desc
    I_carrid import spfli-carrid some
    I_connid import spefi-connid some
    9) save , check and activate…adapter class proposed by system is
    ZCL_IM_IM_LINESEL is genereated.
    IMPLEMENTATION OF BADI DEFINITION
    1) EXECUTE tcode se18.choose menuitem create from the implementation menubar.
    2) Specify aname for implementation ZIM_LINESEL
    3) Specify short desc.
    4) Choose interface tab. System proposes a name fo the implementation class.
    ZCL_IM_IMLINESEL which is already generarted.
    5) Specify short desc for method
    6) Dbl clk on method to insert code..(check the code in “AAA”).
    7) Save , check and activate the code.
    Some useful URL
    http://www.esnips.com/doc/e06e4171-29df-462f-b857-54fac19a9d8e/ppt-on-badis.ppt
    http://www.esnips.com/doc/10016c34-55a7-4b13-8f5f-bf720422d265/BADIs.pdf
    http://www.esnips.com/doc/43a58f51-5d92-4213-913a-de05e9faac0d/Business-Addin.doc
    http://www.esnips.com/doc/1e10392e-64d8-4181-b2a5-5f04d8f87839/badi.doc
    www.sapgenie.com/publications/saptips/022006%20-%20Zaidi%20BADI.pdf
    http://www.sapdevelopment.co.uk/enhance/enhance_badi.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/04/f3683c05ea4464e10000000a114084/content.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/e6/d54d3c596f0b26e10000000a11402f/content.htm
    http://help.sap.com/saphelp_nw2004s/helpdata/en/c2/eab541c5b63031e10000000a155106/frameset.htm
    Now write a sample program to use this badi method..
    Look for “BBB” sample program.
    “AAA”
    data : wa_flights type sflight,
    it_flights type table of sflight.
    format color col_heading.
    write:/ 'Flight info of:', i_carrid, i_connid.
    format color col_normal.
    select * from sflight
    into corresponding fields of table it_flights
    where carrid = i_carrid
    and connid = i_connid.
    loop at it_flights into wa_flights.
    write:/ wa_flights-fldate,
    wa_flights-planetype,
    wa_flights-price currency wa_flights-currency,
    wa_flights-seatsmax,
    wa_flights-seatsocc.
    endloop.
    “BBB”
    *& Report ZBADI_TEST *
    REPORT ZBADI_TEST .
    tables: spfli.
    data: wa_spfli type spfli,
    it_spfli type table of spfli with key carrid connid.
    *Initialise the object of the interface.
    data: exit_ref type ref to ZCL_IM_IM_LINESEL,
    exit_ref1 type ref to ZIF_EX_BADISPFLI1.
    selection-screen begin of block b1.
    select-options: s_carr for spfli-carrid.
    selection-screen end of block b1.
    start-of-selection.
    select * from spfli into corresponding fields of table it_spfli
    where carrid in s_carr.
    end-of-selection.
    loop at it_spfli into wa_spfli.
    write:/ wa_spfli-carrid,
    wa_spfli-connid,
    wa_spfli-cityfrom,
    wa_spfli-deptime,
    wa_spfli-arrtime.
    hide: wa_spfli-carrid, wa_spfli-connid.
    endloop.
    at line-selection.
    check not wa_spfli-carrid is initial.
    create object exit_ref.
    exit_ref1 = exit_ref.
    call method exit_ref1->lineselection
    EXPORTING
    i_carrid = wa_spfli-carrid
    i_connid = wa_spfli-connid.
    clear wa_spfli.
    Reward points for useful Answers
    Regards
    Anji

  • Need help in BADI...full marks wud be given.

    Hi guys,
    i am implementing a badi for a transaction MIRO...i got the BADI which is AC_DOCUMENT ..but this is working fine,but want a badi for iline items..
    can anybody help me in finding the right badi for line items..

    Hi,
    YOu may use the BADIs:
    MB_DOCUMENT_BADI and MB_MIGO_BADI for ur req.
    Pls reward poins if useful.
    Regards,
    Renjith Michael.

  • Help in BADI ADDRESS_SUBSCREEN

    Dear sirs,
    I've implemented the BADI ADDRESS_SUBSCREEN and I'm able to read data from screen by the structure ADDR1_DATA. But If I change the variable ADDR1_DATA the screen fields does not change.
    Here is the code: By debugging this code I can read the initial content from POST_CODE1 and CITY2. But if I change the values nothing happens.
    method IF_EX_ADDRESS_SUBSCREEN~ADDR_APPL_SUB_PBO_EXPORT.
      break-point.
      ADDR1_DATA-POST_CODE1 = '09895-590'.
      ADDR1_DATA-CITY2 = 'CALYX'.
    endmethod.
    Anyone could please help me to solve this?
    Thank you in advance.
    Fabio Purcino

    Hi,
    those parameters are importing so it will not be possible to change these values in method ADDR_APPL_SUB_PBO_EXPORT. I have not implemented this BADI yet but If you go to SE80 and enter the interface IF_EX_ADDRESS_SUBSCREEN you will see all implementations of this interface. In my system I have for example this class CL_IM_FM_ADDRESS_SUBSCREEN. You can look at that code for your inspiration. It may be helpful for you.
    Cheers

  • Help in BADI Creation

    Hi
    I got solution from OSS to create the BADI
    MB_MIGO_BADI (method CHECK_HEADER) to
    check if the date in GOHEAD-BUDAT is greater than the system date
    (SY-DATLO).
    But i have tried this in Tcode Se18 where i write Badi Definition
    MB_MIGO_BADI but it gives error that Badi Definition does not Exist.
    So how should i proceed further.
    Please help me in this regard.
    Thank you,
    Bhaskar

    Hi,
      Goto SE18-> 'MB_MIGO_BADI'-> Click Display. Then goto Transaction SE19 - Type ZMB_MIGO_BADI and click on create, it will ask for Definition name provide 'MB_MIGO_BADI' in it and then provide text. Goto interface tab now and double click on the method you need to code and write your code there.
      Hope this might have helped you.
    Thanks,
    Prashanth

Maybe you are looking for