开发者

Bit Mask Question

开发者 https://www.devze.com 2023-03-11 12:52 出处:网络
Readng the documentation for a point of sale system, here is the example they give for a mask that is supposed to tell you what seats are selected. I开发者_运维知识库 can\'t figure this out. I complet

Readng the documentation for a point of sale system, here is the example they give for a mask that is supposed to tell you what seats are selected. I开发者_运维知识库 can't figure this out. I completely understand bit masking. Is this example just wrong?

Function

This system variable is an eight character string that contains the seat filter mask.

Type/Size

A8

Syntax

@Filter_mask

Filter_mask Usage

This system variable is Read-Only.

Filter_mask Example

The following is an example of a filter mask:

80000000 - seat 1 is active (@filter_active ="Y")
00000001 - seat 32 is active (@filter_active = "Y")
50A00000 - seats 2,4,9 and 11 are active (@filter_active = "Y")
00000000 - filter inactive, no seats


These are 8-digit hexadecimal numbers, which can store as much as a 32-digit binary number.

(16^8 = 2^32 = 4,294,967,296)

In a binary representation, each digit corresponds to a seat:

   hex               binary
80000000 = 10000000000000000000000000000000
00000001 = 00000000000000000000000000000001
50a00000 = 01010000101000000000000000000000
00000000 = 00000000000000000000000000000000

The first binary bit is for seat #1, and the last is for seat #32. Since they're giving you an 8-character string, you'll probably want to parse it into a 32-bit value to do the masking arithmetic. Look for "hex string to integer" in your target language and you'll find something like:

Convert hex string to int in Python

Note: Google can do base conversions off the cuff for you when you're looking at things like this, such as ("0x50a00000 in binary"), but Wolfram Alpha does a bit more:

http://www.wolframalpha.com/examples/NumberBases.html


convert each of the digits one at a time, into 4-bit binary.

80000000 -> 1000 0000 ....
50A00000 -> 0101 0000 1010 ...


The example seems right. They just reversed the most significant and least significant bits around, and used a 1-based index for bit positions (apart from keeping the data in ASCII instead of 4 bytes of hex). So, if bit n is set (LSB being bit #0, or the value 00000001), you get seat number 31-n+1.


Your masks are the following:

Seat 1: 0x80000000
Seat 2: 0x40000000
Seat 3: 0x20000000
Seat 4: 0x10000000
Seat 5: 0x08000000
...
Seat 31:0x00000002
Seat 32:0x00000001

Or more general:

mask = 1 << (32 - seatno)

Example:

0x50A00000 = 0x40000000 | 0x10000000 | 0x00800000 | 0x00200000

hence seat 2, 4, 9 and eleven.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号