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
logind
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 » Fri Nov 23, 2007 8:33 am

hai,

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......

#include<stdio.h>
#include<conio.h>
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;
}e;
e.ch=65;
clrscr();
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);
getch();
}

here im get the Output is: 01000001.

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

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

Post by sclo » Fri Nov 23, 2007 11:20 am

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

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

Post by maxdiver » Fri Nov 23, 2007 1:20 pm

Maybe you need bitwise operations?

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

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

Post by Moha » Thu Dec 06, 2007 1:11 am

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++”