Using Power of 2 numbers to represent types

Let’s say that we have some values, represented by power of 2:

TYPE_1 = 1
TYPE_2 = 2
TYPE_3 = 4
TYPE_4 = 8
...

I need to store some of these types in one value.

Example:
To represent TYPE_1 with TYPE_3, the value is 5.

What operation is used to identify that value 5 represent TYPE_1 with TYPE_3?

2

What operation is used to identify that value 5 represent TYPE_1 with
TYPE_3?

I’m going to assume you mean that you’ve been handed a value (say, 5) and want to know how to tell that the first and third bits are set and therefore represent TYPE_1 and TYPE_3.

There are a couple of ways to handle this. Both involve using a series of logical ANDs to shut off all of the bits in the value you’ve been handed except the one you’re interested in to see if the remaining bit is 1 or 0:

    00000101   The value; 5 in this case
and 00000100   The mask, TYPE_3
    --------
    00000100   The ANDed result, which is nonzero.  TYPE_3's bit is set.

Same thing, but with TYPE_2, which isn’t present in the value:

    00000101   The value; 5 in this case
and 00000010   The mask, TYPE_2
    --------
    00000000   The ANDed result, which is zero.  TYPE_2's bit is not set.

The first way to handle this is to do comparisons against each type:

if ( value & TYPE_1 ) handle_type_1();
if ( value & TYPE_2 ) handle_type_2();
 ...etc...

This is the brute force method, but if you’re dependent on the TYPE_* macros defined in some header file, this is the only way to do it safely. You can’t depend on the bits representing each type not changing. (And shame on whoever wrote the header file for not providing some functions or macros to help you figure these things out.)

If you do have control over the arrangement of the bit field and can declare that bit n represents type n, it becomes easy to put together a few macros (or functions if you’re not using C) to handle it:

#define NUMBER_OF_TYPES       4

/* These are bit numbers, not powers of two */
#define TYPE_1                1
#define TYPE_2                2
#define TYPE_3                3
#define TYPE_4                4

#define TYPE_MASK(t)          (1 << (t-1))
#define VALUE_HAS_TYPE(v,t)   ((v) & TYPE_MASK((t)))

/* Construct a value holding two types */
unsigned value = TYPE_MASK(TYPE_1) | TYPE_MASK(TYPE_3);

for (unsigned type_num=0 ; type_num < NUMBER_OF_TYPES ; ++type_num) {
    if ( VALUE_HAS_TYPE(value, type_num)
        do_something_with_type_number(type_num);
}

This iterates over the number of types you have, checks to see if each one is present and then calls a function with its type number. This gives you a huge amount of flexibility in do_something_with_type_number(), because you can use a switch statement on the type number or use it as an index into an array that might point at functions to handle each type. You can’t do either of those with bitwise comparisons.

If you use the bitwise or operator, you will get the value 5

print(TYPE_1 | TYPE_3) //5

2

I’m using C here, but similar operators exist in most languages.

Assume you have some variable to store the types:

unsigned char mytypes;

 #define TYPE_1  1
 #define TYPE_2  2
 #define TYPE_3  4
 #define TYPE_4  8

To set one of the types, you would use either:

mytypes = mytypes | TYPE_1;

or, as a shorthand in C,

mytypes |= TYPE_1;

in either case, any existing bits (types) present in mytypes are preserved.

To check whether mytypes contains a specific type, you would write:

if ((mytypes & TYPE_1) == TYPE_1)

Similarly, if you wanted to check for two types at once, you would write:

if (((mytypes & (TYPE_1 | TYPE_3)) == (TYPE_1 | TYPE_3))

where TYPE_1 | TYPE_3 equals 5 as per your example.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật