trying to convert system bytes into bits and nibbles.

This is a discussion on trying to convert system bytes into bits and nibbles. within the C Programming forums, part of the General Programming Boards category; i use vim and we have to create this basically creating is as a subroutine Code: /* 2 File: Lab3-5.c ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    trying to convert system bytes into bits and nibbles.

    i use vim and we have to create this basically creating is as a subroutine
    Code:
    /*
          2 File: Lab3-5.c
          3 Course: 
          4 Author: 
          5 Date: Monday January 23, 2006
          6  */
          7
          8 #include <stdio.h>
          9 void datatypes()
         10 {
         11     
         12     
         13   
         14
         15     fprintf(stdout, "Type float on Munro has a size of %u bytes \n",sizeof(float));
         16     fprintf(stdout, "Type double on Munro has a size of %u bytes.\n",sizeof(double));
         17     fprintf(stdout, "Type long double on Munro has a size of %u bytes.\n",sizeof(long double));
         18     fprintf(stdout, "Type short on Munro has a size of %u bytes.\n",sizeof(short));
         19     fprintf(stdout, "Type long on Munro has a size of %u bytes.\n",sizeof(long));
         20
         21     int main()
         22     {
         23         datatypes();
         24         return 0;
         24     }
    I got it to show up thw size of each one there with %u and sizeof right but i dont know how the convert each one of them to bits and nibbles in code. anyone can do it on paper but its the code i tried using other things like int bit and nibble and multiple it out but i dont know how to get it to print the output. .. anyhelp wud be appreciated..
    Last edited by Salem; 01-24-2006 at 12:51 AM. Reason: Remove email address

  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    This compiles? Main needs to be declared outside of datatypes!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    You mean you want to convert Decimel to Binary? Here's a hint: Bit-wise Operators: <<, >>, &

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ok what in the name of spammers sunday are you doing writing to the screen using fprintf!!!!!!!!!!!!!!!!!? i mean it works and all that (when you close the data types function first!) but have you ever used just printf! no need to mention stdout there. fprintf is written primarilly to write to an external file!

    as for what you want to do - are you trying to say how many bits and nybbles (spelt with a y) there are in said data types?

    ever heard of multiplication!!? 8 bits in a byte, 2 nybbles in a byte also - the smallest unit of data in ANSI C is the byte, with the exception of bitwise operators, whereby you are always manipulating bits of a specified variable, all of which are 1 byte (or more) in memory

    Nevertheless, this might be a wilder tangent than the one above, but if you're asking how to break these values up into strings of binary data, you're barking up the wrong tree - these values which you are getting are a representation of how much space a data type is allocated in memory - its not actually like you can break it into ones and zeros - its not relevant data - 4 is "100" in binary, and always will be, so its a bit futile writing code to convert it into its individual bits. i hope for god's sake that this was not what you were thinking - otherwise i'm sorry to say that there's no hope for you

    just in case you havent heard of how to multiply, here's what your code would look like - using just printf

    Code:
    printf ("Type float on Munro has a size of %u bytes, %u nybbles, and %u bits\n",sizeof(float), 2*(sizeof(float), 8*(sizeof(float));
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    A main() inside a function!?! Does this work?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    sorry i mistakely took out the closing} with the subroutine so u guys thought it was the main function inside of that subroutine and that wudnt make it run but sorry bout that. and also for the spelling error or *nybble i was alittle sleepy. As for using fprintf i dont have a clue because i had another teacher that taught me printf and then this one taught me fprintf and stdout.

    *Richie T* is that bad code that i wrote? Do i need to only use printf?


    i didnt even know that you cud multiply it like that but thank you so much.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by Richie T
    bits and nybbles (spelt with a y)
    I don't mean to nitpick, but I believe you're falsely correcting someone. I usually see it spelled "nibble" rather than "nybble" and Wikipedia seems to agree with me:
    A nibble (or less commonly, nybble) is the computing term for the aggregation of four bits, or half an octet (an octet being an 8-bit byte).
    If you understand what you're doing, you're not learning anything.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    [QUOTE=kraze_505]
    *Richie T* is that bad code that i wrote? Do i need to only use printf?
    QUOTE]

    I know I'm not "Richie T", but I think I can answer this question.

    If you want to print to a screen the easiest way to do so is to use printf. I think fprintf(parameters) is mainly used for printing to a text document .... http://www.cprogramming.com/tutorial/cfileio.html

    specifically about half ways down:

    fprintf(fp, "Testing...\n");

    That prints to a document which is specified by the pointer fp the string "Testing..." and a newline return.

    I agree with that other guy too about the nybbles, it's kind of like dyslexia and dysfunctionality ... and nobody likes them but that said, it is right all the same.

  9. #9
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    A trick for figuring out the actual number of bits in an unsigned char.
    Code:
    int bits_in_uchar(void)
    {
        unsigned char byte = 0;
        int count = 0;
        byte = ~byte;   /* set all the bits to 1 */
        while (byte != 0)
        {
            count++;
            byte = byte >> 1;
        }
        return count;
    }
    The above should work regardless of how signed values are handled, and regardless of word/char size. This does not mean that this returns the number of bits in the smallest machine addressable unit, but it usually does.
    Insert obnoxious but pithy remark here

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ok perhaps i was a bit harsh with the spelling issue, but i think that the person/persons who came up with the concept had a sense of humour about spelling byte (in a world of microchips - bad puns everywhere), and extending that to the smaller unit, nybble seems a logical choice - at the end of the day its writing a good program that counts and so long as we all have an idea of the approximate name for 4 bits, i think we'll all survive. with that said, i'll stick to my spelling and probably be told off repeatedly about misspelling it in the future!

    as regards fprintf - i dont know how much more or less efficient it is than printf at runtime, but i do know that printf was speciffically designed to be used ro write to stdout always, and the only difference between it and fprintf is that the latter can be redirected, more specifically to an external file, as described by twomers. printf is definitely shorter to type than directing fprintf to stdout - thats probably why it was coded, as a quick, always easy to use shorthand version of this case of fprintf. it was simply a shock to the system that you had encountered this method of writting to the console, and you hadn't encountered operations on function arguments, namely where i took

    Code:
    2*sizeof(float)
    the fact is that you can do lots of different types of operations on arguments, typically just addition and so on provided you use parentheses as appropriate!

    in the line of code i submitted, there were errors with parentheses, if you tried to compile it, it wouldn't work. here it is as corrected - notice that you dont really need any here, it would be more of a case were you wanted to execute a formula with lots of operations. i also want to be clear that i dont imply that you need to take more care with parentheses here than normal, the opposite is what i'm trying to get across! try changing your code to this:

    Code:
    printf ("Type float on Munro has a size of %u bytes, %u nybbles, and %u bits\n",sizeof(float), 2*sizeof(float), 8*sizeof(float));
    now let the matter be called complete - everything mentioned in this thread has been discussed at length!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by filker0
    A trick for figuring out the actual number of bits in an unsigned char.
    CHAR_BIT
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    thank you everyone and *richie T* that code you gave me at first well i just saw the 2 and the 8 and it clicked so i just put it in like how u had it in the second time u posted. that means i didnt really look at the first post to notice that you had (sizeof(float)) . anyway i immediately put it like how u had it in the second time and it worked fine. good code. i guess our professor wants us to use fprint and stdout for later on when we have to output to a file. i guess he just wants to build on it. but then again i see the shortness of printf alone.

    thanks everyone!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bits, Bytes & Nibbles!
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 10:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21