Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default How do i convert a int to a binary with bitwise operators only?

    How do i convert a int to a binary with bitwise operators only?

    And the output must have 32bits place

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What you exactly means by bitwise operator? What's your exact requirement also, because to convert the int into binary string you have several other approaches as well.

  3. #3
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    i was told to use only bitwise operation
    such as & | >> >>> << ~
    to convert int to binary.

    there cannot be multiplication n division used in the algorithm.

    any clue?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,572
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    How do i convert a int to a binary with bitwise operators only?

    And the output must have 32bits place
    pst, don't tell them I told you but most (if not all) digital computers already store integers in binary format. No need to fiddle with them any further.

    kind regards,

    Jos

  5. #5
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    ok, thks for informing
    but that doesn't solve my problem

    pls advise me how to print it in binary... 10101010111
    such that i have to use bitwise operators only.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,572
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by diskhub View Post
    ok, thks for informing
    but that doesn't solve my problem

    pls advise me how to print it in binary... 10101010111
    such that i have to use bitwise operators only.
    There are a couple of (static) methods in the Integer class that can help you.

    kind regards,

    Jos

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Are you asking how to get a String representation of the binary value of an int?

    To do that you need to look at each bit position of an int and test if its a 0 or a 1.
    Use the AND operator for that.
    Remember: 1 AND 1 = 1 1 AND 0 = 0

    To look at all 32 bit positions, use the shift operator to move to the next position.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    No need to fiddle with them any further.
    That's why I'm worried too Jos. Trying to somethings which already implemented, and which is easily could be solve in a messy way.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by diskhub View Post
    ok, thks for informing
    but that doesn't solve my problem

    pls advise me how to print it in binary... 10101010111
    such that i have to use bitwise operators only.
    Yeah look around toBinaryString(int val) on the API for Integer class.

    But still I'm confusing that why you really do that.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    It sounded to me like a student's assignment to learn how to use bitwise operators. As an assembler programmer, I think it's good to know what's under the covers.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    It sounded to me like a student's assignment to learn how to use bitwise operators. As an assembler programmer, I think it's good to know what's under the covers.
    Of course it could be. But my concern is that someone guide him in a very messy way.

  12. #12
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    so no guide?
    i thought of one... but might not be a prof way...

    that's why i wanted to see what u expert can do... nevermind, i guess u guys are also not used to this tricky question. :D

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No lol. Everyone here in the forum wants to help, including me. But the way you got the question is bit a work. That's what we want you to pointed.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here comes the messy part there, from the API

    Java Code:
    private static String toUnsignedString(int i, int shift) {
    	char[] buf = new char[32];
    	int charPos = 32;
    	int radix = 1 << shift;
    	int mask = radix - 1;
    	do {
    	    buf[--charPos] = digits[i & mask];
    	    i >>>= shift;
    	} while (i != 0);
    
    	return new String(buf, charPos, (32 - charPos));
        }
    Can you understand that code segment?

  15. #15
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    i am thinking of doing this... not sure whether this is good?

    for every binary place of a NUM


    if (NUM-(NUM>>1<<1)==1)
    print "1"
    else
    print "0"

    NUM= NUM>>1

    LOOP

    but i want to hear from you guys what plan u have?

  16. #16
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    can u explain the following line of ur code:

    int radix = 1 << shift; //what's the shift in this case? what to put for the arg Shift?

    digits[i & mask]; // what abt this?

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by diskhub View Post
    can u explain the following line of ur code:

    int radix = 1 << shift; //what's the shift in this case? what to put for the arg Shift?

    digits[i & mask]; // what abt this?
    shift = 1, think about it.

    As I said earlier this is what Integer class does.

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by diskhub View Post
    for every binary place of a NUM
    Can you please tell me that how you get the binary place in a number (I hope NUM means an integer value)

  19. #19
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default

    interesting... i will explore it.
    thanks for ur advice.

    how's my method, any chance?

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by diskhub View Post
    interesting... i will explore it.
    thanks for ur advice.

    how's my method, any chance?
    I've a question for you, in my last post.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 10-26-2009, 10:17 PM
  2. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  3. convert binary to images
    By fiqueudrue in forum New To Java
    Replies: 3
    Last Post: 02-12-2009, 09:16 AM
  4. Using the bitwise operators
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:39 PM
  5. Use recursion to convert binary to...
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •