Sign in to follow this  

Expand a bit into a bit

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question that may ( or may not ) be easy. I need to be able to expand a bit into a byte, and visa versa, in C/C++. I have tried this on my own, and I know it envolves shifting, but I have edited and re-edited my code so much that I'm afraid its junk now, and I'm so confused I don't now what to do. If anyone could help me out, I would mucho appreciate ;)

Share this post


Link to post
Share on other sites
Define "expand". You can't store individual bits in a PC, the smallest adressible unit is a byte. So you can have 8 bits in one byte, but they're already as a byte, and can't get expanded.

What exactly are you trying to do?

Share this post


Link to post
Share on other sites
I have trouble understanding what you mean by "expanding", but I suspect this could give you an idea:

unsigned char byte_of_bit(int i, unsigned b)
{
return (i & 1 << b) ? 0xf : 0x00;
}

void bit_of_byte(int &i, unsigned b, unsigned char by)
{
if (by == 0xff) i = i | 1 << b;
if (by == 0x00) i = i & ~(1 << b);
}

Share this post


Link to post
Share on other sites
Sorry, I haven't slept to much lately, that's probably why I'm so incoherent. Initially, I was just trying to get a bit out of a unsigned integer and expand it into an array of 32 unsigned char values. Does that help?

Share this post


Link to post
Share on other sites

[code]unsigned char daBytes[32]; // sizeof(unsigned int)*8
unsignged int daInt=666;

void bit2Byte(
  unsigned char *daBytes,
  unsinged int daInt
  )
{

  // 32=sizeof(unsigned int)*8
  for (int i=0;i<32;i++)
  {
    daBytes[i]=(daInt >> i) & 1;
  }
}[/code]

Voila! You have your bits in a byte array. Enjoy!

Edited by jbadams
Restored post contents from history.

Share this post


Link to post
Share on other sites
Here is my code so far. All I did was take one line of your code and made ( more like tried to it seems ) it compatible with my own. The only function that matters is BExpand. Sorry for the undo fustration.

#include <stdio.h>
#include <conio.h>

void BExpand (unsigned char *results, int length, unsigned int bits)
{
*results = ( bits >> length ) & 1;
}


void PrintBinary (unsigned char *results, int length)
{
//Represents nibble length; used for spacing.
unsigned int NIBBLE_LENGTH = 0;
//Variable used to iterate through for loop.
int Iterator = 0;

//For each bit...
for ( ; Iterator < length; Iterator++ ) {

//...Print a one to screen if the bit is on, or a zero to screen if it is off...
*results & ( 1 << Iterator ) ? printf ( "%d", 1 ) : printf ( "%d", 0 );
//...If the length of a nibble has been reached, print a space.
NIBBLE_LENGTH == 3 ? printf ( " " ), NIBBLE_LENGTH = 0 : NIBBLE_LENGTH++;

};

//If we have not completed a nibble...
if ( Iterator % 4 != 0 ) {

do {

//...print a dash...
printf ( "-" );
//...and increment the iterator...
Iterator++;

//...until we have finished the nibble.
} while ( Iterator % 4 != 0 );

};

//Insert a new line when we are done.
printf("\n");
}

void main()
{
unsigned char data [32];
unsigned int bits = 0xDEADBEEFL;
int len;

for (len = 32; len > 0; len -= 7)
{
BExpand (data, len, bits);
PrintBinary (data, len);
}

_getch();
}

Share this post


Link to post
Share on other sites

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this