# Bit(field) macros [Core]

## Simple bit macros

#define BIT(n)   ( 1<<(n) )
Create value with bit n set.
#define BIT_SHIFT(a, n)   ( (a)<<(n) )
Shift a by n.
Create a bitmask len bits long.
#define BIT_SET(y, flag)   ( y |= (flag) )
Set the flag bits in word.
#define BIT_CLEAR(y, flag)   ( y &= ~(flag) )
Clear the flag bits in word.
#define BIT_FLIP(y, flag)   ( y ^= (flag) )
Flip the flag bits in word.
#define BIT_EQ(y, flag)   ( ((y)&(flag)) == (flag) )
Test whether all the flag bits in word are set.
Create a bitmask of length len starting at bit shift.
#define _BF_GET(y, shift, len)   ( ((y)>>(len))&(shift) )
Retrieve a bitfield mask of length starting at bit shift from y.
#define _BF_PREP(x, shift, len)   ( ((x)&BIT_MASK(len))<<(shift) )
Prepare a bitmask for insertion or combining.
#define _BF_SET(y, x, shift, len)   ( y= ((y) &~ BF_MASK(shift, len)) | _BF_PREP(x, shift, len) )
Insert a new bitfield value x into y.

## some EVIL bit-field operations, >:)

These allow you to mimic bitfields with macros. Most of the bitfields in the registers have foo_SHIFT and foo_SHIFT macros indicating the mask and shift values of the bitfield named foo in a variable. These macros let you prepare, get and set the bitfields.

#define BFN_PREP(x, name)   ( ((x)<<name##_SHIFT) & name##_MASK )
Prepare a named bit-field for for insterion or combination.
#define BFN_GET(y, name)   ( ((y) & name##_MASK)>>name##_SHIFT )
Get the value of a named bitfield from y. Equivalent to (var=) y.name.
#define BFN_SET(y, x, name)   (y = ((y)&~name##_MASK) | BFN_PREP(x,name) )
Set a named bitfield in y to x. Equivalent to y.name= x.
#define BFN_CMP(y, x, name)   ( ((y)&name##_MASK) == (x) )
Compare a named bitfield to named literal x.
#define BFN_PREP2(x, name)   ( (x) & name##_MASK )
Massage x for use in bitfield name with pre-shifted x.
#define BFN_GET2(y, name)   ( (y) & name##_MASK )
Get the value of bitfield name from y, but don't down-shift.
#define BFN_SET2(y, x, name)   ( y = ((y)&~name##_MASK) | BFN_PREP2(x,name) )
Set bitfield name from y to x with pre-shifted x.

## Functions

INLINE u32 bf_get (u32 y, uint shift, uint len)
Get len long bitfield from y, starting at shift.
INLINE u32 bf_merge (u32 y, u32 x, uint shift, uint len)
Merge x into an len long bitfield from y, starting at shift.
INLINE u32 bf_clamp (int x, uint len)
Clamp to within the range allowed by len bits.
INLINE int bit_tribool (u32 flags, uint plus, uint minus)
Gives a tribool (-1, 0, or +1) depending on the state of some bits.
INLINE u32 ROR (u32 x, uint ror)
Rotate bits right. Yes, this does lead to a ror instruction.

## Function Documentation

 INLINE u32 bf_get ( u32 y, uint shift, uint len )

Get len long bitfield from y, starting at shift.

Parameters:
 y Value containing bitfield. shift Bitfield Start; len Length of bitfield.
Returns:
Bitfield between bits shift and shift + length.

 INLINE u32 bf_merge ( u32 y, u32 x, uint shift, uint len )

Merge x into an len long bitfield from y, starting at shift.

Parameters:
 y Value containing bitfield. x Value to merge (will be masked to fit). shift Bitfield Start; len Length of bitfield.
Returns:
Result of merger: (y&~M) | (x<<s & M)
Note:
Does not write the result back into y (Because pure C does't have references, that's why)

 INLINE int bit_tribool ( u32 flags, uint plus, uint minus )

Gives a tribool (-1, 0, or +1) depending on the state of some bits.

Looks at the plus and minus bits of flags, and subtracts their status to give a +1, -1 or 0 result. Useful for direction flags.

Parameters:
 flags Value with bit-flags. plus Bit number for positive result. minus Bit number for negative result.
Returns:
+1 if plus bit is set but minus bit isn't
-1 if minus bit is set and plus bit isn't
0 if neither or both are set.

Generated on Mon Aug 25 17:03:57 2008 for libtonc by 1.5.3
{"ft_lang_label":"__label__en","ft_lang_prob":0.5056188,"math_prob":0.95891935,"size":3543,"snap":"2020-45-2020-50","text_gpt3_token_len":1105,"char_repetition_ratio":0.15993218,"word_repetition_ratio":0.13213703,"special_character_ratio":0.34010726,"punctuation_ratio":0.1502146,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9891678,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-23T11:56:41Z\",\"WARC-Record-ID\":\"<urn:uuid:e699c017-edc9-4e8f-a7df-edc80971077d>\",\"Content-Length\":\"21155\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7e449d19-b6c1-46d4-a3e1-b9cf250baa80>\",\"WARC-Concurrent-To\":\"<urn:uuid:d01a3795-d121-40e8-8980-ad8738e377bf>\",\"WARC-IP-Address\":\"78.46.86.37\",\"WARC-Target-URI\":\"https://www.coranac.com/man/tonclib/group__grpCoreBit.htm\",\"WARC-Payload-Digest\":\"sha1:BYU4W7W3DINSSCXWHKKKIQZYIC3DRXV5\",\"WARC-Block-Digest\":\"sha1:B4PW6LOT5H25BF5ZGPTNC3KPFURTUALK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107881369.4_warc_CC-MAIN-20201023102435-20201023132435-00014.warc.gz\"}"}