How to single 8 bit Varialbles Store in Single Bytes

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
New poster
Posts: 1
Joined: Thu Nov 22, 2007 10:27 am

How to single 8 bit Varialbles Store in Single Bytes

Post by logind »


here im showing my program, in the Structure declare single unsigned bit 8 variables,the structure will be call my union function, my doubt is how to store this seperate 8 variables single bit Value in single byte. Give Some other suggestion for How Single bit 8 Values Store in Singe Byte . please help me yar......

void main()
struct num{
unsigned isolate1:1;
unsigned open1:1;
unsigned short1:1;
unsigned fire1:1;
unsigned isolate2:1;
unsigned open2:1;
unsigned short2:1;
unsigned fire2:1;
union d{
struct num n;
char ch;
printf("%d %d %d %d %d %d %d %d",e.n.fire2,e.n.short2,e.n.open2,e.n.isolate2,e.n.fire1,e.n.short1,e.n.open1,e.n.isolate1);

here im get the Output is: 01000001.

so this seperate 8bit values store in single byte how is it possible sir/madam.

Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Location: Vancouver, BC, Canada

Post by sclo »

Sorry, I don't understand your problem, can you explain more clearly?

Learning poster
Posts: 51
Joined: Tue Sep 04, 2007 2:12 pm
Location: Russia, Saratov

Post by maxdiver »

Maybe you need bitwise operations?

int bit1 = byte & 1;
int bit2 = (byte >> 1) & 1;
int bit3 = (byte >> 2) & 1;
int bit4 = (byte >> 3) & 1;

Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran

Post by Moha »

Of course it should be. It is one of the advantages of C. But pay attention the size of num struct is not 1byte. it is 4bytes.

Post Reply

Return to “C++”