Become a MacRumors Supporter for $25/year with no ads, private forums, and more!

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
Is there something equivalent to this psuedocode below in Objective-C?

Code:
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

Moderator emeritus
Jun 25, 2002
14,568
645
Lard
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.
 

Jordan72

macrumors member
Original poster
Nov 23, 2005
88
0
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.

Code:
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

Moderator emeritus
Jul 24, 2002
25,611
889
Harrogate
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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

Moderator emeritus
Jul 24, 2002
25,611
889
Harrogate
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

macrumors 6502
Dec 2, 2005
488
151
Roanoke, VA
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

macrumors member
Original poster
Nov 23, 2005
88
0
bearda,

I tried:

Code:
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

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Jordan72 said:
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

macrumors member
Original poster
Nov 23, 2005
88
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.