Memory Questions

Started by
8 comments, last by clrscr 22 years, 2 months ago
An int is 4 bytes, well is there a way to make each one of those bytes hold somthing different, like 4 different flags or something like that. "A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila." -Mitch Ratliffe
-----------------------------When men speak of the future, the Gods laugh.An apology for the devil: it must be remembered that we have heard one side of the case. God has written all the books.Samuel Butler (1835 - 1902)
Advertisement
I know this can be done with the bit shifting operators << and >> but those have always confused me and I''m not 100% sure how to use them.
Why not just use byte instead?
Well, if you just want a true/false value you can use bitwise operators and you''ll have 32 flags in one int:

int a = 0;

a |= 0x01; //set bit
a |= 0x02;
a |= 0x04;
etc...

a &= 0x01; //unset bit

a ^= 0x01; //flip bit

if((a & 0x01) == 0x01) //see if bit is set

Sorry if you already know this...





-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
Sure!

     // byteValue1, byteValue2 byteValue3 and byteValue4   // are char values   // Assignment   int values = byteValue1 |               ( byteValue2 << 8 ) |               ( byteValue3 << 16 ) |               ( byteValue4 << 24 );   // Single assignment   // First clear the position while maintaing the   // integrity of the other values.  Then make   // the assignment   values &= 0xFF00FFFF;   values |= byteValue3 << 16;   // Access   byteValue1 = ( char )( values & 0x000000FF );   byteValue2 = ( char )( ( values & 0x0000FF00 ) >> 8 );   byteValue3 = ( char )( ( values & 0x00FF0000 ) >> 16 );   byteValue4 = ( char )( ( values & 0xFF000000 ) >> 24 );  



or

long lmyNumber = 0;
char *myLongNumberInBytes = NULL;

lmyNumber = 255324;
myLongNumberInBytes = &lmyNumber;
myLongNumberInBytes[0] = 0;
myLongNumberInBytes[1] = 0;
... etc.

Remember that this code will act up if you try to port to a machine that uses a different endianness.


quote:a &= 0x01; //unset bit

No.
  a &= ~0x01;  


The easiest thing to do is to use bitfields, as it saves all the dicking about with masks and whatnot.

People complain that they''re "slow" or somesuch, but they tend to optimize into the same thing as manually masking bits. So I''m not sure how much truth there is in that.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
  	int myInt = 0; // clear all bytes		for(int i = 0; i < 4; i++)	{		((char*)&myInt)[i] = i + 1;		cout << (__int8)((char*)&myInt)[i]  << " ";	}	cout << endl << myInt << endl;  


this is called hacking for the most part, if this is a school project, your teachers would probably not like it... there are ''safer'' ways to do it.
Or you could use a union:

union int4 {	int val;	unsigned char c[4];}int4; 


by setting val to the int in question, you can use the four parts of c.


-
"I do not approve of anything that tampers with natural ignorance. Ignorance is like a delicate exotic fruit. Touch it and the bloom is gone."
Lady Bracknell in "The Importance of being Earnest" by Oscar Wilde
union, nice greg : why didnt i think of that!
Thanks for all your help

"A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila."
-Mitch Ratliffe
-----------------------------When men speak of the future, the Gods laugh.An apology for the devil: it must be remembered that we have heard one side of the case. God has written all the books.Samuel Butler (1835 - 1902)

This topic is closed to new replies.

Advertisement