Re: [dev] newbie question

From: Adam Kandur <rndd_AT_tuta.io>
Date: Wed, 28 Oct 2020 12:23:11 +0100 (CET)

thank you for the answer



Oct 28, 2020, 11:15 by jona.a99_AT_gmail.com:

> Hello Adam,
>
>> hi everyone, i'm new in c programming. i tried to read sources but stuck with enum.
>> as i know, enum is mainly used to assign names to integral constants. but i don't understand, for example, this code
>> ----------------------------------------------
>> enum term_mode {
>>     MODE_WRAP        = 1 << 0,
>>     MODE_INSERT      = 1 << 1,
>>     MODE_ALTSCREEN   = 1 << 2,
>>     MODE_CRLF        = 1 << 3,
>>     MODE_ECHO        = 1 << 4,
>>     MODE_PRINT       = 1 << 5,
>>     MODE_UTF8        = 1 << 6,
>> };
>> ----------------------------------------------
>>
> I am assuming you don't know what '<<' does?
> The '<<' Operator (as well as '>>') is a bit shifting operator.
> In a nutshell, it shifts the binary representation of the number on the
> left by the number of places on the right.
> This is equivalent to taking the number on the right and multiplying
> that often by 2 (or dividing in case of '>>').
> For a better explaination see the GNU C manual:
> https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Bit-Shifting
>
>> why not simply write ?
>> ----------------------------------------------
>> enum term_mode {
>>     MODE_WRAP        = 1,
>>     MODE_INSERT      = 2,
>>     MODE_ALTSCREEN   = 4,
>>     MODE_CRLF        = 8,
>>     MODE_ECHO        = 16,
>>     MODE_PRINT       = 32,
>>     MODE_UTF8        = 64,
>> };
>> ----------------------------------------------
>>
> This is completely equivalent in terms of generated machine code.
> I would argue however, that the first case is easier for humans to read
> (if you understand the operator) and write.
> Especially for higher powers of 2, it is much easier to make a mistake.
>
> Regards,
> Jona
>
Received on Wed Oct 28 2020 - 12:23:11 CET

This archive was generated by hypermail 2.3.0 : Wed Oct 28 2020 - 12:24:09 CET