How to create mask in a machine independent way?

1.2k views Asked by At

So I'm practicing some programming interview questions, and stumbled across this sample pdf which recommends "Understand how to use masks and create them in an machine independent way". But it doesn't eloborate the difference between machine dependent and machine independent mask.

I usually just figure out the integer that provides the mask that I want, for example if I only want the last 4 bits I would do:

int y = x & 15;

I don't understand why this would be machine dependent, if it is.

So what is an example of creating a mask that is machine independent? And what is an example of creating a mask that is machine dependent?

Perhaps what they are talking about is what if you need a mask for something that isn't an integer, in which case my approach wouldn't work (I've never needed a mask for anything except for integers)?

1

There are 1 answers

0
TedPoch On

I believe that "machine independent" here means that your code should perform the desired operation (e.g. a mask and shift) regardless of the compiler and/or machine on which it runs. For example, different compilers and systems treat the length of data types differently. If you write a bitshift on ints that presumes a size of 4 bytes, this would not be "machine independent". Some compilers treat ints as 8 bytes.