Expand a bit into a bit

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

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 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 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 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 on other sites
Well, the easiest approach would probably be dump your int into a std::bitset and just read off from that.

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!

Restored post contents from history.

Share on other sites
That actually doesn't work. If it was correct, you should still be able to get 666, I'm getting 888 when I try to verify.

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();
}

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×