Results 1 to 20 of 29
 05232010, 05:41 AM #1Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
 05232010, 02:36 PM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
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.
 05232010, 02:39 PM #3Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
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?
 05232010, 04:09 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
 05232010, 04:13 PM #5Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
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.
 05232010, 04:18 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
 05232010, 04:22 PM #7
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.
 05232010, 05:24 PM #8
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
 05232010, 05:30 PM #9
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
 05232010, 05:34 PM #10
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.
 05232010, 05:43 PM #11
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
 05242010, 12:45 AM #12Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
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
 05242010, 01:52 AM #13
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
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.
 05242010, 01:59 AM #14
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
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)); }
 05242010, 02:00 AM #15Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
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?
 05242010, 02:05 AM #16Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
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?
 05242010, 02:11 AM #17
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
 05242010, 02:13 AM #18
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
 05242010, 02:13 AM #19Member
 Join Date
 May 2010
 Posts
 25
 Rep Power
 0
interesting... i will explore it.
thanks for ur advice.
how's my method, any chance?
 05242010, 02:15 AM #20
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
Similar Threads

how to convert decimal value into 8bit binary value
By tOpach in forum New To JavaReplies: 4Last Post: 10262009, 11:17 PM 
Convert binary into decimal
By WarmRegards in forum New To JavaReplies: 8Last Post: 10182009, 02:32 PM 
convert binary to images
By fiqueudrue in forum New To JavaReplies: 3Last Post: 02122009, 10:16 AM 
Using the bitwise operators
By Java Tip in forum java.langReplies: 0Last Post: 04172008, 07:39 PM 
Use recursion to convert binary to...
By coco in forum Advanced JavaReplies: 1Last Post: 08072007, 07:46 AM
Bookmarks