PDA

View Full Version : Does printf() or NSLog() print Binary?




Jordan72
Dec 8, 2005, 01:20 AM
Is there something equivalent to this psuedocode below in Objective-C?

printf("My binary number: %bin", my32BitData);

I want to see the binary equivalent of some data during runtime. Is it possible to do this without creating my own conversion method or function?



bousozoku
Dec 8, 2005, 02:02 AM
I haven't seen anything other than a scripting language such as REXX that will do the conversion.

However, since it's not a big deal, you should be able to find the conversion code somewhere online or be able to create it in 10 minutes or fewer.

robbieduncan
Dec 8, 2005, 04:12 AM
%x will convert to hex but there does not appear to be a similar conversion for binary.

Edit to add: NSLog basically uses fprintf (http://www.opengroup.org/onlinepubs/007908799/xsh/fprintf.html).

Jordan72
Dec 8, 2005, 09:21 AM
Yes, I could write a method or function that would do what I want. I was just hoping I wasn't missing anything.

Anybody know what's going on underneath the hood here? For example, I assume that an integer that is a statically declared value in my code exists as a binary value.


int myInt = 2;

//in my code it looks like this: 0000 000 0000 0010


Does someone know the kinds of operations that have to convert this value to use it? In other words, change it to some other format for use, maybe a moniter so it can be printed out or a "type converstion"? What about saving to files and other things? Do conversions exists that programmers should know about that would create mini-bottlenecks in code for ASCII, UTF-8 or integers?

robbieduncan
Dec 8, 2005, 09:28 AM
The compiler will covert a statically declared 2 to it's binary representation. You should not worry too much about very low level conversions like this. They are unlikely to have any major impact on performance.

The bigest performance gains will come from using better algorithms.

caveman_uk
Dec 8, 2005, 10:35 AM
Jordan72 you do seem a little obsessed about optimisations and minutiae that really aren't going to help you that much. As robbieduncan (and others have pointed out in another thread), a better algorithm with a half-decent implementation will almost always beat a lousy algorithm with an optimised implementation. If you've got a great algorithm and it's still too slow THEN worry about what the compiler is actually doing or if the objective-C overhead between an NSArray and an NSMutable array is going to be a problem.

There's always too much stuff to do when coding and fixing stuff that ain't broke is a waste of time (mostly)

robbieduncan
Dec 8, 2005, 11:17 AM
Without wanting to be unkind I'd say you are suffering from premature optimisation! If you are trying to optimise your code have you profiled it (say with Shark). If not there is a real chance you are optimising the wrong section of code anyway!

bearda
Dec 8, 2005, 11:27 AM
I don't know much about Objective-C, but in ANSI C you can use the %b flag for a binary print (although using %x to print it in hex tends to be a lot more useful).

Andrew Beard

Jordan72
Dec 8, 2005, 10:20 PM
bearda,

I tried:

printf("myInt: %b", x);

and it didn't work. It's written in C. I compiled it using gcc.

caveman,

I don't understand what you mean by comparing algorithm and implementation. I just understand the term implementation represents a section of code as the only place where algorithms occur(in OOP). I thought of an algorithm as being something that might be one of the elements of the implementation. So, I dont' understand your comparison: "a better algorithm with a half-decent implementation".

It seems, although I could be wrong, that you are implying that implementation has some clever design that exists outside the category algorithm, but I'm scratching my head trying to figure it out. What do you mean? I'm new, so I'm not used to all these new terms and the lingo yet.

caveman_uk
Dec 9, 2005, 02:39 AM
I don't understand what you mean by comparing algorithm and implementation. I just understand the term implementation represents a section of code as the only place where algorithms occur(in OOP). I thought of an algorithm as being something that might be one of the elements of the implementation. So, I dont' understand your comparison: "a better algorithm with a half-decent implementation".

OOP is irrelevant to this discussion.

An algorithm is the design abstracted from the actual programming. It can be in English or pseudo-code, a flowchart or anything. It is independent of the actual programming language.

The implementation is the actual code. The way you 'implement' the algorithm.

For example there are a number of 'classic' algorithms for sorting data. Two are called 'quicksort' and 'bubblesort'. The quicksort is slightly harder to implement that the bubblesort. You could have the best programmed, most highly optimised bubblesort ever but a half decent unoptimised quicksort will nearly always be faster. The quicksort is a better design...a better algorithm. The actual implementation doesn't matter here. The actual algorithm (which could be implemented in ANY language) is better.

Check out http://www.algosort.com to see more about types of algorithms
and http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/quick/quicken.htm for stuff about quicksort

Jordan72
Dec 9, 2005, 10:52 AM
Ok, I thought about that and it makes sense. Thanks for pointing out that distinction. I've recoginized the concept while I've programmed, but I didn't know the terminology.