You could try a union with a packed structure..If you using MS you'll have to Google to find out how to pack a structure...
#include <stdio.h>
union mu
{
int x;
struct
{
unsigned int a:8;
unsigned int b:24;
}__attribute__((packed)) in;
};
int main(void)
{
union mu theu;
theu.x = -1;
fprintf(stdout, "%x\n", theu.in.b);
return 0;
}
Output
ffffff
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
That sounds even more convoluted.
You could clear out the bits you don't want: i = i & 0xFFFFFF; to make the value 24 bits.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
That sounds even more convoluted.
You could clear out the bits you don't want: i = i & 0xFFFFFF; to make the value 24 bits.
I'm not trying to start an argument but your solution still has the original amount of bits, its not 24 bits but 32 bits with a masking operation..Maybe I misunderstood the op?
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
As the OP said,
I'm trying to get it to output fewer bits: e.g. 24-bit: FFFFFF In fact, the output I want needs to be 6 digits in all cases.
Clearing out the top 8 bits effectively gives you a 24 bit value. Therefore, 6 digits can be output.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
For example, try this:
#include <stdio.h>
int main()
{
int i=-1;
for (i=5; i > -5; i--)
printf("%06X\n", i & 0xFFFFFF);
return 0;
}
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
WaltP I don't disagree with your reasoning, I'm just stating that the variable is still sizeof(int) in size...Its probably cutting hairs but that's just the way I am..
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0