#### Archived

This topic is now archived and is closed to further replies.

# Memory Questions

This topic is 6013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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 );

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
  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.

##### Share on other sites
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

##### Share on other sites
union, nice greg : why didnt i think of that!

##### Share on other sites

"A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila."
-Mitch Ratliffe

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631752
• Total Posts
3002091
×