0
typedef NS_OPTIONS (NSInteger, YYTextLineStyle) {
// basic style (bitmask:0xFF)
YYTextLineStyleNone       = 0x00, ///< (        ) Do not draw a line (Default).
YYTextLineStyleSingle     = 0x01, ///< (──────) Draw a single line.
YYTextLineStyleThick      = 0x02, ///< (━━━━━━━) Draw a thick line.
YYTextLineStyleDouble     = 0x09, ///< (══════) Draw a double line.

// style pattern (bitmask:0xF00)
YYTextLineStylePatternSolid      = 0x000, ///< (────────) Draw a solid line (Default).
YYTextLineStylePatternDot        = 0x100, ///< (‑ ‑ ‑ ‑ ‑ ‑) Draw a line of dots.
YYTextLineStylePatternDash       = 0x200, ///< (— — — —) Draw a line of dashes.
YYTextLineStylePatternDashDot    = 0x300, ///< (— ‑ — ‑ — ‑) Draw a line of alternating dashes and dots.
YYTextLineStylePatternDashDotDot = 0x400, ///< (— ‑ ‑ — ‑ ‑) Draw a line of alternating dashes and two dots.
YYTextLineStylePatternCircleDot  = 0x900, ///< (••••••••••••) Draw a line of small circle dots.
};

This code is what I see on a framework, generally enumerated values use decimal, but this code use hexadecimal, what is the benefit?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Dwyane
  • 23
  • 2
  • 2
    You get a better readable enum set and you can use bits, e.g. to be send to low-level controlers, for easy testing, etc. See the comment that says `bitmask 0xFF` – Paul Ogilvie Dec 19 '18 at 08:46
  • 1
    May be of interest: https://stackoverflow.com/q/34904564/2442804 - basically: to be able to perform proper bitwise arithmetics. – luk2302 Dec 19 '18 at 08:55
  • For anything dealing with bits, it is more convenient to use a base that is a power of 2 (which decimal, 10, isn't) since each digit in such a base represents an integer number of bits. In base 16 (hexadecimal), each digit corresponds to exactly 4 bits, and therefore a byte of 8 bits can be written in exactly two hexadecimal digits. Alternatively, for individual bits, the syntax `1 << n` is often seen, where `n` denotes the number of the bit (zero being the least significant bit). – Arkku Dec 19 '18 at 12:10

3 Answers3

2

It just usable to see the bits:

in hex format you can see the bits easily:

0x0100 | 0x11 = 0x0111

in decimal you don't:

256 | 17 = 273

Community
  • 1
  • 1
SHR
  • 7,940
  • 9
  • 38
  • 57
  • Thank you. How about using the following bit operation? YYTextAttributeTypeUIKit = 1 << 0, YYTextAttributeTypeCoreText = 1 << 1, – Dwyane Dec 19 '18 at 09:22
0

Answer option a:
Ask the author.

Answer option b (my guess):
It is about readability as perceived by the author.
They might consider values which are in the context/vicinity of a lower-byte bitmask (see inside the () in the comments) more readable in hex, even if they are not even passing the lowest nibble. In the context of a second set of values, which are in relation to a high-byte bit-mask and DO pass the lower byte, using hex for all of them is more conistent and hence helps readability.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

Using bit then you can combine multiple enums by bitwise or | operation.

This is useful when there are multiple orthogonal properties that can exist at the same time.

For example, YYTextLineStyleThick | YYTextLineStylePatternDot will draw a thick line with dotted pattern.

llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • For what you demonstrate, defining them with hex representation is not functionally necessary - if the value is the same, e.g. 256*1, 256*2, ... – Yunnosch Dec 19 '18 at 09:02
  • @Yunnosch Nothing is *functionally necessary* if you want to write "256*1, 256*2, ..." – llllllllll Dec 19 '18 at 09:50