Memory Questions
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
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
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
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.
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.
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:
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 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement