Does printf() or NSLog() print Binary?

Discussion in 'Mac Programming' started by Jordan72, Dec 7, 2005.

  1. macrumors member

    Joined:
    Nov 23, 2005
    #1
    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?
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    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.
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    %x will convert to hex but there does not appear to be a similar conversion for binary.

    Edit to add: NSLog basically uses fprintf.
     
  4. thread starter macrumors member

    Joined:
    Nov 23, 2005
    #4
    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?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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.
     
  6. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    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)
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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!
     
  8. macrumors 6502

    Joined:
    Dec 2, 2005
    Location:
    Germantown, MD
    #8
    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
     
  9. thread starter macrumors member

    Joined:
    Nov 23, 2005
    #9
    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.
     
  10. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #10
    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
     
  11. thread starter macrumors member

    Joined:
    Nov 23, 2005
    #11
    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.
     

Share This Page