Mayank's C/C++ Challenger - IV

Hi All,

Kindly excuse my prolong absence from the forum. Here comes our own quite old C/C++ Challenger, with another not very great but a bit mind twisting C/C++ puzzle.

So It goes as:

Write an efficient way to print 32 bitwise representation of an integer exactly as stored in the memory[O/p should be in Binary]. Make sure the Output of your program should differ if it is made to run on different hardwares.[One supporting Little Endian eg: Intel and another Supporting Big Endian eg: SPARC format]. If required, you can have the flexibility to use integer size of 16 bit instead of 32 bit.

Example:
[For 32 bit Integer]
I/P: 5
O/P:
Big Endian:
00000000000000000000000000000101
Little Endian:
00000101000000000000000000000000



Regards,
Mayank Shukla

Replies

  • pradeep_agrawal
    pradeep_agrawal
    Here comes a solution :smile::

    void printbits(int num)
    {
    for(int i=1, int j=1; (i|j)!=0; (i&&j)?(i=((j=i)<<1))πŸ˜”j?(j=((i=j)&0)):1), j?1πŸ˜›rintf("%d",(num&i)?1:0), j?1πŸ˜”i=((i>>1)&~i)));
    }


    The above function will print all the bits of an integer [irrespective of the hardware, and size of int (works for both 16-bit and 32-bit int)] 😎.

    Pradeep "Crazy" Agrawal
  • pradeep_agrawal
    pradeep_agrawal
    Some text was converted to smilies. So here is the edited code.

    void printbits(int num)
    {
    for(int i=1, int j=1; (i|j)!=0; (i&&j) ? (i=((j=i)<<1)) : (j ? (j=((i=j)&0)) : 1), j ? 1 : printf("%d",(num&i)?1:0), j ? 1 : (i=((i>>1)&~i)));
    }
  • pradeep_agrawal
    pradeep_agrawal
    A more optimized and simple solution πŸ˜‰:

    void printbits(int num)
    {
    for(unsigned int i = ~(((unsigned int)-1)/2); i; printf("%d", num&i ? 1 : 0), i /= 2);
    }


    Pradeep "Crazy" Agrawal 😁
  • DEP
    DEP
    :arrow: gve me a code executing 'if' && 'else' statements simultaneously...........



  • Kaustubh Katdare
    Kaustubh Katdare
    Re: $!mP|_e 0ne !--!--!!

    DEP
    :arrow: gve me a code executing 'if' && 'else' statements simultaneously...........



    meant esp for😁 Mr. aggarwal
    DEP, welcome to the board. Go through the CE Common Sense Guide (Link in my signature) and lectures #1 - #4 in our newbie training center.

    I'm unable to figure out if this is a 'new question' or just extention of Mayank's Challenger -IV. Also, do not throw questions at any particular member unless it is required. Make sure you title your thread appropriately.

    I hope that doesn't scare you πŸ˜€ . Looking forward to see you around.

    -The Big K-
  • DEP
    DEP
    Re: $!mP|_e 0ne !--!--!!

    i got ur point...............

    the question i posted is a new one n nt an extension to the challenger.........


    hope i am clear..........
  • sahana
    sahana
    pradeep or mayank.
    explanation for the code please.
  • uday.bidkar
    uday.bidkar
    pradeep_agrawal
    A more optimized and simple solution πŸ˜‰:

    void printbits(int num)
    {
    for(unsigned int i = ~(((unsigned int)-1)/2); i; printf("%d", num&i ? 1 : 0), i /= 2);
    }
    Pradeep, both the solutions you posted wont work as expected by Mayank. Both will print the actual binary representation of the number irrespective of their memory representaion ( Little or Big Endian wont make a defference ) as the operators ( <<, &, ~ ) that have been used would consider actual number irrespective of the memory representatoins to produce a result.

    Suggession: Process the number byte by byte instead of processing it as a whole.
  • Mayank
    Mayank
    Hi Everyone,

    Uday is exactly right in saying that the code would just print the binary representation of the number used and is platform independent...that is what I dont want. In the question it clearly mentions that the O/P on different platform should be different, on the basis of the methodology used to store data in memory.
    Your program is giving the same O/P i.e 00000000000000000101 on both SPARC systems and on INTEL systems, when run with I/P as '5'.

    Sorry for me replying late to this thread, the reason or excuse as you people might want to call it is: There was a weekend in between and I was not having a INTEL system installed with C++ compiler[so dumb of me]. Had checked on SPARC systems the day when the reply was posted. But on SPARC as it supports BIG ENDIAN and the O/P seemed fine.

    Whatever it is..... but I must appreciate the level of response and the quality of code produced. The reason it is not producing o/p as expected can be that the question was not clear enough.
    But, I m highly impressed with the quality code posted, another reply to the same with an intension to optimized performance and then one of our crazy engineer pointing out the exact problem..... That's really great and this is exactly the way i want things to proceed on CE.

    **I apologize for this unnecesary and long reply to this post πŸ˜•.


    Thanks and Regards,
    Mayank Shukla.
  • Mayank
    Mayank
    sahana
    pradeep or mayank.
    explanation for the code please.
    Hi Sahana,

    I think if you try to really look into the code, giving it few cool minutes.... you yourself will find the cause behind it ;-) . Anyways, when the solution is confirmed from everybody's side then the one posting the same should ideally post the explanation too. I mean, that is what is expected 😁.


    Thanks and Regards,
    Mayank Shukla.
  • uday.bidkar
    uday.bidkar
    Mayank
    Write an efficient way to print 32 bitwise representation of an integer exactly as stored in the memory[O/p should be in Binary]. Make sure the Output of your program should differ if it is made to run on different hardwares.[One supporting Little Endian eg: Intel and another Supporting Big Endian eg: SPARC format]. If required, you can have the flexibility to use integer size of 16 bit instead of 32 bit.
    Mayank Shukla
    Modifying Pradeep's second solution to work as expected by Mayank.

    void PrintBits( int num )
    {
    for( int i = 0 ; i < (int)(((int*)0)+1) ; i++ )
    {
    for(int j = 0x80; j; printf("%d", (((char*)&num))&j ? 1 : 0), j /= 2);
    }
    }

    Explaination:
    (int)(((int*)0)+1) will give the number of bytes in the integer ( to make it work for both 16 bit and 32 bit integers ). So outer for loop helps to process the number byte by byte.
    Inner for loop takes the number one byte at a time ( (((char*)&num)) gives ith byte ) and prints bit value for each bit position. j is initialized with 0x80 i.e. binary 10000000 and ANDed with the byte of number under consideration. j /= 2 will shift this 1 in j to right in each iteration to check the bit value of next bit in the byte.

    Regards,
    Uday

You are reading an archived discussion.

Related Posts

CEans! The Crazy Engineers (yeh, that's us!) Rock! 😁 Its a pleasure to announce that CE & a youth magazine based out of city of Pune (India), called "Poora Poona"...
Please Help Me Out frenzz.... I'm new to this community...and anyhow i'm posting this thread. I'm in Btech 2nd year and want to hav some summer training frm any software...
It's funny to see all over the world people using ASP or PHP, or even JSP to make server programmation. It seems to me that we should give a change...
CEans, This (read the title) has been a point of concern ever since the inception of CE Forums. How can we build a professional-yet-fun-loving community of engineers? It doesn't make...
Hi All , Today I must welcome you all for a detailed discussion on energy security for our country INDIA. Post your veiws as earliest. Regards, vibhor_one