So, I'm working with Java, and testing out various bitwise functions, shifts, whatnot. When I do whatever bitwise operations, I want to be able to see things on a bit level - that is, after doing:

Code:
byte a = (byte) 0b10011001;
a = ~a;
System.out.print( a );


Instead of having it display 01100110 like I would want it to, it displays 102. Is there a routine somewhere that displays a decimal number as a binary one?

EDIT: Welp, this is embarrassing. At the time of writing I had written a routine that does what I want, but it had some errors; so I posted about it, and fixed the errors less than five seconds after making the post. Anyways, here's the routine.


Code:
public static String Binarify( byte ByteToCheck ) {
        String binaryCode = "";
        byte[] reference = new byte[]{ (byte) 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
       
        for ( byte z = 0; z < 8; z++ ) {
            //if bit z of byte a is set, append a 1 to binaryCode. Otherwise, append a 0 to binaryCode
            if ( ( reference[z] & ByteToCheck ) != 0 ) {
                binaryCode += "1";
            }
            else {
                binaryCode += "0";
            }
        }
       
        return binaryCode;
    }


The routine accepts a byte as a parameter, and returns a string version of the binary digits. If someone has a much better/more optimized way of doing this, please let me know!
Too bad you're not using Python to turn integer x into a string:


Code:
binstring = "".join(map(lambda n: "1" if (x&(1<<n)) else "0",range(ceil(log(x,2)))))


The optimal Java form would be pretty close, I think. You should make the mask be your iteration variable, in my opinion, rather than the bit position.
KermMartian wrote:

Code:
binstring = "".join(map(lambda n: "1" if (x&(1<<n)) else "0",range(ceil(log(x,2)))))
Ugh.
Code:
binstring = bin(0x2f)
# '0b101111'
binstring = bin(0x2f)[2:].rjust(8,'0')
# '00101111'

Would be better to have a single variable as your mask, initialize to 0x80 and simply shift right (watch for sign extension). Also somewhat faster to use a StringBuilder preallocated to the right size rather than storing a raw String (which involves creating a new object every time you append to it).
Yeah, benryves will tell you and I'll be the first to confirm that I love re-inventing the wheel. I was pretty confident that I was solving a solved problem as far as Python, but I charged ahead anyway. Also, Lincoln, are you sure you're always dealing precisely with 8-bit numbers?
Kerm, in Java, isn't a byte guaranteed to be 8 bits? And, for the original question, a quick google search found http://stackoverflow.com/questions/5263187/how-can-i-print-a-integer-in-binary-format-in-java

So, basically:

Code:
public static String binarify(byte b) {
  return Integer.toBinaryString((int)b);
}
Or, just use ints and Integer.toBinaryString() Razz
This doesn't seem to be working for me. This is exactly how I thought I should do it, but it is still printing in base 10. Here is the code. Any help would be appreciated. It's not a big deal because it's just for debugging, but it's really annoying me.

byte[] sys_mem = m_sysinfo.getSystemMemory();
int index = frame_num * bytes_per_page;
byte b1 = sys_mem[index];
byte b2 = sys_mem[index + 1];
Debug.user(" 0:" + Integer.toBinaryString((int)b1));
Debug.user(" 1:" + Integer.toBinaryString((int)b2));

This prints 0:0 and 1:0
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement