Thread: showbits()???

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    showbits()???

    in a book i saw that there is function built into c that displays the binary value of a number, it looks something link this
    showbits(int);
    but when i tried it, didnt work, do you know which header file i have to include to enable this?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    26
    I have never heard of this function 'showbits', but from its name and the sintax I believe that there might be a workaround, or another function that does the same thing as this function. I know that the function '_itoa(string, number, base)' in MSVC++ 6, can be used to display the binary value of a integer in a string. I don't know if this will work in other compliers but here is an example of the usage of _atoi for MSVC++.

    Code:
    #include <cstdlib>
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    
    int main()
    {
    	char buffer[20];
    	int  i = 3445;
    
    	_itoa( i, buffer, 10 );
    	cout << "String of integer " << i << " (radix 10): " << buffer << endl;
    	_itoa( i, buffer, 16 );
    	cout << "String of integer " << i << " (radix 16): " << buffer << endl;
    	_itoa( i, buffer, 2  );
    	cout << "String of integer " << i << " (radix 2): " << buffer << endl;
    	return 0;
    }
    The output is as follows.
    Code:
    String of integer 3445 (radix 10): 3445
    String of integer 3445 (radix 16): d75
    String of integer 3445 (radix 2): 110101110101
    -JLBshecky
    System
    OS - Microsoft Windows XP Pro
    CPU - AMD Athlon XP 2600+
    Mother Board - Abit KV7
    RAM - 512 Mb DDR (333)

    C++
    Microsoft Visual Studio Pro. 6.0
    MSDN July 2001

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by JLBSchreck
    I know that the function '_itoa(string, number, base)' in MSVC++ 6, can be used to display the binary value of a integer in a string. I don't know if this will work in other compliers but here is an example of the usage of _atoi for MSVC++.

    Code:
    snip c++ code
    -JLBshecky
    Which might be fine if they were using C++. However, this is the C board.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Originally posted by quzah
    Which might be fine if they were using C++. However, this is the C board.

    Quzah.
    Replace all cout statements with fitting printf statements and there you are. _itoa is plain C.


    But this is the cross-platform C forum, not the MS forum
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CornedBee
    Replace all cout statements with fitting printf statements and there you are. _itoa is plain C.
    Not really. _itoa is Microsoft. the underscore is the clue. Just this alone tells me that itoa() is not a standard function.

    ANSI C only defines ato?() functions, not the other way around. MS usually puts an underscore in front of functions that are not ANSI Standard, whereas Borland does not.

    Many compilers though have defined ?toa() functions, but you can't rely on them for portability.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not really.
    Plain C != Standard C.

    This is why I posted this part:
    But this is the cross-platform C forum, not the MS forum.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. showbits
    By sanjeev_ddas in forum C Programming
    Replies: 6
    Last Post: 09-07-2006, 01:36 AM