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

1. Member
Join Date
May 2010
Posts
25
Rep Power
0

## 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. 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. Member
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?

4. Originally Posted by diskhub
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. Member
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.

6. Originally Posted by diskhub
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. 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. Originally Posted by JosAH
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. Originally Posted by diskhub
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. 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. Originally Posted by Norm
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. Member
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

13. 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. 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;
do {
i >>>= shift;
} while (i != 0);

return new String(buf, charPos, (32 - charPos));
}```
Can you understand that code segment?

15. Member
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?

16. Member
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?

17. Originally Posted by diskhub
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. Originally Posted by diskhub
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. Member
Join Date
May 2010
Posts
25
Rep Power
0
interesting... i will explore it.

how's my method, any chance?

20. Originally Posted by diskhub
interesting... i will explore it.

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

Page 1 of 2 12 Last

#### Posting Permissions

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