Archived

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

clrscr

Memory Questions

Recommended Posts

clrscr    158
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 this post


Link to post
Share on other sites
Rudan    158
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 this post


Link to post
Share on other sites
The Bear    122
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
DrPizza    160
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 this post


Link to post
Share on other sites
EvilCrap    134
  
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 this post


Link to post
Share on other sites
gregs    364
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 this post


Link to post
Share on other sites
clrscr    158
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

Share this post


Link to post
Share on other sites