Binary representation of a number, binary operators.

This is a discussion on Binary representation of a number, binary operators. within the C Programming forums, part of the General Programming Boards category; Hi, I have to deal with a binary representation of a number, starting with most significant bit. The problem is ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    Binary representation of a number, binary operators.

    Hi,
    I have to deal with a binary representation of a number, starting with most significant bit. The problem is that I don't have enough memory to keep the whole binary representation so I can't just simply convert the number in base 10 to it's binary representation. Is it possible to get every bit from some number starting from MSB (using binary operators) so I don't have to keep whole number's binary representation in memory ?
    Regards.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    You mean you don't have enough memory to keep an array of 16, 32 or 64 booleans? (C99)

    Anyways, one way you can do this, assuming the value can be antything from a char to a double, is to build a for loop of as many as steps as your number size in bits.

    Then you shift left with the operator <<, taking the MSB. You calculate the mask from your position in the for loop.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thank you for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-28-2009, 03:34 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  4. Binary representation in C
    By @nthony in forum C Programming
    Replies: 25
    Last Post: 11-09-2007, 11:43 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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