Recursion with binary numbers

I need to print out a list of all possiblle binary numbers with as many digits as an int k. For example, if k = 3, it would need to print out 000,001,010,011,100,101,110,111. for k = 2 it is only 00, 01, 10, 11. It is pretty clear that for any k, a recursive technique can be used to solve this problem, but I'm having trouble finding the solution. Please help. Thanks

You don't need recursion:for (int i = 0, max = 1 << (k - 1); i < max; ++i) {
  print binary i to k digits
}But a recursive approach would be void increment (int[] data, int digit) {
if (digit > 0) {
--digit;
for (int i = 0; i < 2; ++i) {
  data[digit] = i;
  increment(data, digit);
} else {
print contents of data array
start (int k) { increment(new int[k], k); }Neither code tested.
Pete

Similar Messages

  • Need help with binary numbers

    hey i'm doing some things, but i need a entry of a binary coded number, i know that an hex number is written like 0xFF but how do i put a binary number in an instruction such as:
    byte b
    b="binary number"

    I see what you want to do, but you can only do this with octal and hex. With binary, you would have to do it something like this:
    byte b = Byte.parseByte("01101001", 2); // base-2

  • Playing with binary numbers

    Hi
    I am messing around with classes. I have a card class, which has a rank (2 - 14) and suit (0-3). What I am trying to do, is have a variable in the class cardvalue which holds a binary value for each card, for example, the 2 (2) of hearts (0) would be 0010 00 (I will of course drop the space), I would like it formatted with the first four digits representing the rank, a the second two the suit, for simple comparison with other cards later on in the game.
    How do I take the card values and concatenate them together?
    Many thanks, Ron

    Thanks, I have this:
    [code
    public class Card
         public enum Rank
         Deuce(2),Three(3),Four(4),Five(5),Six(6),Seven(7), Eight(8), Nine(9), Ten(10), Jack(11), Queen(12), King(13), Ace(14);     
              private final Integer facevalue;
         Rank(Integer facevalue)
         this.facevalue = facevalue;
         public Integer facevalue(){ return facevalue; };
         public enum Suit
              Hearts (0), Clubs(1), Diamonds(2), Spades(3);
              private final Integer suitvalue;
              Suit(Integer suitvalue)
                   this.suitvalue = suitvalue;
              public Integer suitvalue(){ return suitvalue; };
         private final Rank rank;
         private final Suit suit;
         private Integer binaryvalue;//value of the card as a 6 bit number, first two suit, second four rank value
         private Card(Rank rank, Suit suit)
    this.rank = rank;
    this.suit = suit;
    //binaryvalue = rank.facevalue & suit.suitvalue;
    //binaryvalue = Integer(Integer.toBinaryString( rank.facevalue));
         public Rank rank() { return rank; }
    public Suit suit() { return suit; }
    public Integer binaryvalue(){ return binaryvalue; }
    public String toString() { return rank + " of " + suit; }
    ]

  • Operations on very large binary numbers

    Hi guys,
    I'm trying to write a java class for manipulations of very large binary numbers.
    I'm representing the BN internally as boolean[] (array of booleans false for 0 & true for 1).
    I want to write an algorithm for the following operations :
    shifLeft (boolean[] b,int n) : shifting binary number to left by n positions.preferably a circular shift .
    and also the corresponding shiftRight() method.
    can some one guide me on how to implement this ?
    thanks.

    Consider an array of ascii chars....
    array =>   | |a|b|c|d|
    offset = 1
    len = 4In the above there are actually five spots in the
    array but the offset points to the second position in
    the array and the length is 4.
    If I was to extract the value it would be "abcd"
    because of the offset and the length.
    Now a 'shift right' means that if it is "abcd" then
    it should now be "abc" (because the d fell off the
    end.)
    I can do that like this.
    array =>   | |a|b|c|d|
    offset = 1
    len = 3Notice in the above that nothing changed except the
    length. But because the length changed if I
    extracted the value I would get "abc" because the
    offset is 1 and the length is three.
    Notice also that there was no array copying.Are you sure shifting works like that ?
    what I know about left shifting is that the 'd' should be moved left -toghether with a, b, c- and we append a 0 in place of 'd' .
    from what you said the 'd' would be gone , or am I again wrong ?
    it seems i still didn't get any satisfactory answer to this problem...

  • Compare 2 Binary Numbers

    Hi all ,
    suppose I have two arbitrary lenght binary numbers (could be very large binaries).
    exple : 1000111 & 11111110001
    is it possible to compare them using only there 0s & 1s representations.
    i.e I don't want to convert them to base 10 & compare them (probably using BigInteger).
    what I'm looking for is an algorithm that compares 1000111 & 11111110001 only using there 0s & 1s sequences.
    If this is possible how can I implement it ?
    many thanks.

    boolean equal = true;
    for(inti = ar1.length, j=ar2.lenght; i >= 0 && j
    =
    0; i--, j--) {
    if(ar1[ i ]!=ar2[j]) {
    equal = false;
    break;
    } this code will tell you if ar1 & ar2 are equal but
    won't tell you if ar1 <ar2 or ar1>ar2
    how can we tell ar1 > ar2 for exple ?oh my bad.
    well then indeed one would hav to start at the highest bit and embed the "shorter" value with 0's.
    if the representation is in a primitive, eg. long, you cant simply write a < b since java doesnt know unsigned numbers. so either you are carefull enough and only store 31 bits in an int(like leave the highest bit alone) or you check the highest bit first, and then check the remaining.

  • Recursive with clause

    Hi All,
    I am using oracle 11.2.0.4
    I m using this for learning purpose
    Below is my table and insert statement
    CREATE TABLE NUMBERS(NUM NUMBER);
    INSERT INTO NUMBERS VALUES(1);
    INSERT INTO NUMBERS VALUES(2);
    INSERT INTO NUMBERS VALUES(3);
    INSERT INTO NUMBERS VALUES(4);
    WITH RSFC(ITERATION,RUNNING_FACTORIAL) AS
    (SELECT NUM AS ITERATION,
    1 AS RUNNING_FACTORIAL
    FROM NUMBERS
    WHERE NUM=1
    UNION ALL
    SELECT R.ITERATION+1,
            R.RUNNING_FACTORIAL * B.NUM
            FROM RSFC R INNER JOIN NUMBERS B
            ON (R.ITERATION+1) + B.NUM    
            SELECT ITERATION,RUNNING_FACTORIAL
            FROM RSFC
    I am learning recursive with clause
    when I am trying to execute the query I am getting
    ORA-00920 : invalid realtional operator
    what is wrong in this query,please help me
    Thanks and Regrds,
    Subho

    Hi,
    2937991 wrote:
    Hi All,
    I am using oracle 11.2.0.4
    I m using this for learning purpose
    Below is my table and insert statement
    CREATE TABLE NUMBERS(NUM NUMBER);
    INSERT INTO NUMBERS VALUES(1);
    INSERT INTO NUMBERS VALUES(2);
    INSERT INTO NUMBERS VALUES(3);
    INSERT INTO NUMBERS VALUES(4);
    WITH RSFC(ITERATION,RUNNING_FACTORIAL) AS
    (SELECT NUM AS ITERATION,
    1 AS RUNNING_FACTORIAL
    FROM NUMBERS
    WHERE NUM=1
    UNION ALL
    SELECT R.ITERATION+1,
            R.RUNNING_FACTORIAL * B.NUM
            FROM RSFC R INNER JOIN NUMBERS B
            ON (R.ITERATION+1) + B.NUM   
            SELECT ITERATION,RUNNING_FACTORIAL
            FROM RSFC
    I am learning recursive with clause
    when I am trying to execute the query I am getting
    ORA-00920 : invalid realtional operator
    what is wrong in this query,please help me
    Thanks and Regrds,
    Subho
    The error actually has nothing to do with the WITH clause.
    Join conditions (that is, the conditions following the ON keyword) must be expressions that evaluate to TRUE or FALSE.  The join condition you posted, however
    (R.ITERATION+1) + B.NUM   
    evaluates to a NUMBER.  The following would be a valid join condition:
    (R.ITERATION+1) = B.NUM  
    but I have no idea if that's what you wanted or not.

  • Help needed with binary data in xml (dtd,xml inside)

    I am using the java xml sql utility. I am trying to load some info into a table.
    my.dtd:
    <!ELEMENT ROWSET (ROW*)>
    <!ELEMENT ROW (ID,JPEGS?)>
    <!ELEMENT ID (#PCDATA)>
    <!ELEMENT DESCRIPTION EMPTY>
    <!ATTLIST DESCRIPTION file ENTITY #REQUIRED>
    <!NOTATION INFOFILE SYSTEM "Files with binary data inside">
    <!ENTITY file1 SYSTEM "abc.jpg" NDATA INFOFILE>
    xml file:
    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE ROWSET SYSTEM "MY.DTD">
    <ROWSET>
    <ROW>
    <ID>1272</ID>
    <DESCRIPTION file="file1"/>
    </ROW>
    </ROWSET>
    I am using the insertXML method to do this. However, the only value that gets loaded is the ID. abc.jpg is in the same directory where I ran the java program.
    Thanks in advance.

    Sorry! wrong dtd. It should read this instead:
    my.dtd:
    <!ELEMENT ROWSET (ROW*)>
    <!ELEMENT ROW (ID,DESCRIPTION?)>
    <!ELEMENT ID (#PCDATA)>
    <!ELEMENT DESCRIPTION EMPTY>
    <!ATTLIST DESCRIPTION file ENTITY #REQUIRED>
    <!NOTATION INFOFILE SYSTEM "Files with binary data inside">
    <!ENTITY file1 SYSTEM "abc.jpg" NDATA INFOFILE>
    null

  • I have 1 apple id, 2 5S iPhones with separates numbers and both ring when I get a call on one of the numbers. How do I switch that of ?

    I have 1 Apple ID, 2 5S iPhones (1 private/1 work) with separates numbers and both phones ring when I get a call on one of the numbers. How do I switch that of ?

    I realize that my wife could make her own iTunes account; however, she's been using mine for about 2 years now and this hasn't been an issue.  I guess with the Family Sharing she can have access to all of our music and apps now with her own account. 
    I'll have to see if unchecking our emails resolves this issue.

  • I bought a new iphone and as i was setting my apple ID account and i signed in to my account and confirmed two useres with diffrent numbers that were signed to the same apple ID and it messed up my i messege and face time. how do i fix that ?

    i bought a new iphone and as i was setting my apple ID account and i signed in to my account and confirmed two useres with diffrent numbers that were signed to the same apple ID and it messed up my i messege and face time. how do i fix that or delete one user ?

    Hello sapiii96,
    Thanks for using Apple Support Communities.
    For more information on this, take a look at:
    iOS: Troubleshooting FaceTime and iMessage activation
    http://support.apple.com/kb/ts4268
    To sign out of your Apple ID
    Go to Settings > FaceTime, tap your Apple ID, and select Sign Out.
    Go to Settings > Messages > Send & Receive, tap your Apple ID, and select Sign Out.
    Best of luck,
    Mario

  • HT4528 Can I put the same information on two different i-phones with different numbers and different ID's?

    Can I put the same information on two different i-phones with different numbers and different ID's?

    Yes and no it really depends on what information you are putting on the devices.

  • Application Express 3.1 + BI-Publisher + problem with formating numbers

    Hello together!
    I use the Oracle BI Publisher Template Builder for Word (10.1.3.4) to generate RTF-Templates. I upload these templates in Oracle Apex (Advanced support-->BI-Publisher/OC4J as print service).
    It works well, but I have a problem with formating numbers.
    In Template Builder I define following number formats, for example: #.##0 for numbers like 1.454.234 and #.##0,00 for numbers like 54,80
    In Template-Builder Preview it looks well.
    But whatever I do, in use with Oracle Apex dots and comma are allready interchances in the printout.
    That means,
    1.454.234 become 1,454,234 in PDF-Report
    54,80 become 54.80 in PDF-Report
    Other than that, the layout is exactly the same like in Template Builder defined.
    What's wrong?
    Do I have to change any country parameter?
    Juliane

    I also had the same problem. I tried with normal formating of 99g99g999d99 instead of ##,##,##0.00 and it has resulted correct way.

  • Creation of sub item with serial numbers in outbound delivery

    Hi All,
    We need to create a sub item for an Outbound Delivery main item in change mode with serial numbers for sub item.
    Process flow:
    1. Sales order item created with main item
    2. Outbound Delivery created with main item
    3. Outbound Delivery interfaced to external logistics sytem
    4. External logistics system pick the main item and a sub item (packaging) from warehosue and ship to customer
    5. External logistics sytems send delivery pick confirmation interface with main item details and serial numbers for sub item
    Now we have to create a sub item with serial numbers in the delivery referencing the same main item.
    NB: Delivey can have multiple similar main items but there is only material number in SAP for sub item. Hence the requirement to identify relevant sub item serial numbers to each delivery main item.
    Can you kindly tell me in what possible ways we can create sub item with serial numbers for a deliveyr main item in change mode.
    Thanks in advance
    Best  Regards
    Veer

    Hi,
    Does anybody have an answer?
    Thanks in advance.

  • DTW General Entry (Goods Receipt) with Serial numbers

    Hi all,
    I want to import 540 items with serial numbers using the General Entry templates. Some items have as many as 200 serial numbers. Total number of serial numbers for 540 items are about 24,000.
    In the Serial Numbers template, will I have to cover 24,000 rows? Is it possible to auto-create the serial numbers with DTW?
    In which field should I put the serial numbers - Internal or System?
    Thanks,
    Ajay Audich

    Hi Ajay Audich,
    You may check this thread first:
    Re: Upload inventory transfer trnsaction with DTW
    Thanks,
    Gordon

  • Goods Receipt with serial numbers

    Hello everyone
    I'm having problems when i try to add a goods receipt with serial numbers
    Code:
            OReceipt = oCompany.GetBusinessObject(BoObjectTypes.oInventoryGenEntry)
            OReceipt.Series = 18
            OReceipt.DocDate = Date.Now
            OReceipt.TaxDate = Date.Now
            OReceipt.Lines.ItemCode = "MP.PP.241"
            OReceipt.Lines.Quantity = 1
            OReceipt.Lines.SerialNumbers.InternalSerialNumber = "SN-01"
            OReceipt.Lines.SerialNumbers.ManufacturerSerialNumber = "SN-01"
            OReceipt.Lines.AccountCode = "_SYS00000000109"
            OReceipt.Lines.WarehouseCode = "MPfabrik"
            OReceipt.Comments = ".NET"
            res = OReceipt.Add()
    Any help will be greatly appreciated!

    Hi Nestor
    All forums on SCN contain a description of what the forums is for. This forum's  Overview page:
    Looking at the overview, I suspect you want to post your query elsewhere? Perhaps one of  Microsoft's forums?
    - Ludek
    Senior Support Engineer AGS Product Support, Global Support Center Canada
    Follow us on Twitter

  • How to automate the adding of check-box along with each numbered item in Framemaker file ?

    I tried using the wingdings font as check-box. But I don't want to use it manually every time. I want to have it as a style part of my procedure style (numbering style).   I need the check-box along with each numbered item. Whenever I apply my numbering style in FM, I should get something like:
    " <checkbox> 1. <text> "

    Use "N:\u2751\ <n+>.\ " for the autonumber format.
    "N" Use counter N
    "\u2751" One of many checkboxes available in Unicode
    "<n+>" increment the counter
    "." ordinary period
    "\ " non-breaking space
    This assumes that you are on FM8 or later, and your Unicode font has that glyph code point populated (Arial Unicode MS does if your font doesn't).
    Don't use WingDings (or Zaph Dingbats) anymore. Using codepage overlays has any number of issues, and these two sets of legacy codepage dingbats don't even match each other.

Maybe you are looking for