Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create mask in a machine independent way?

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)?

like image 735
danglingPointer Avatar asked Jul 16 '17 23:07

danglingPointer


1 Answers

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.

like image 62
TedPoch Avatar answered Dec 17 '22 09:12

TedPoch