Skip to contentSkip to conversation

value assignment

DISCUSSION Codex 3 participants 3 messages Sep 10, 2002

lore
Share & tools
lore
#1 lore raw
In reply to Codex
Quoted 6 lines · Codex

pterm.iflag &= ~(IGNBRK | INLCR)

what the hell is '&=' and '~(...)', what does '(...)' does anyway..?, wasn't '|' means OR..?

Good morning,

So, they are all bitwise operations. & is bitwise AND, | is OR, ~ one's complement. Do you know what happens when you say i*=5 (i=i*5), the same thing when you say i&=5 (i=i&5).
Say i is 15 then i&=5 is 001111 & 000101 = 000101. ~ is a little bit more complecated, that is when the size of your variable is important. If i is an integer (of course the size of i depends on your system, but generally say 32 bits) then ~i (when i is 15) will be
~0000000000001111=1111111111110000=fff0 Hex.
So long,

Mehran

#2 lore raw
In reply to Codex
Quoted 7 lines · Codex

hey i never know what kind assignment is this in C, kindof strange i don't get it at all, i somehow know this when doing <termios.h>

pterm.iflag &= ~(IGNBRK | INLCR)

what the hell is '&=' and '~(...)', what does '(...)' does anyway..?, wasn't '|' means OR..?

<left> &= <right> is equivalent to <left> = <left> & <right> K&R 2nd Ed. page 50

| is bit-wise inclusive or. ibid at page 48

~ is ones complement. ibid at page 48

(...) form a sub-expression, in which at execution time the stuff inside the parens are calculated first. ibid at page 53.

So, reading the statement from innermost to outermost, it goes like this:

1)  Calculate the inclusive OR of symbols IGNBRK and INLCR.  (Because these symbols represent constants and not variables in this context, the compiler will perform this operation at compile time.)
2)  Take the ones-complement of the value calculated in (1).  Because the above calculation involves only constants, this operation will also be performed at compile time.
3)  Calculate the AND of the value calculated in (2) and pterm.iflag.
4)  Store the result of (3) into pterm.iflag.

For single-address stack-oriented machines, the approximate code generated would be:

LoadConstant Compile-time-value-of-(2)
AND pterm.iflag
STORE pterm.iflag

If you don't have a copy of Kernighan and Ritchie's _The C Programming Language_ (Prentiss-Hall, ISBN 0-13-110362-8) then I suggest you run, not walk, to your nearest bookstore and pick up a copy. Or Amazon if you prefer to shop online, at http://www.amazon.com/exec/obidos/ASIN/0131103628/qid=1031671821/sr=2-1/ref=sr_2_1/103-8245374-9473450.

There is no excuse for any student trying to pick up the C language lacking a copy of this relatively inexpensive textbook, as it still serves as the "standard" for practitioners. (The ANSI standard is almost unreadable.)

Get it, and READ pages 1 through 190. Several times. DON'T skim.

Stephen Satchell

Codex

author
#0 lore raw

Again, Newbie ask

hey i never know what kind assignment is this in C, kindof strange i don't get it at all, i somehow know this when doing <termios.h>

pterm.iflag &= ~(IGNBRK | INLCR)

what the hell is '&=' and '~(...)', what does '(...)' does anyway..?, wasn't '|' means OR..?

---------------------------------------------------------------------------

Quoted 3 lines

Telepon anda sibuk, panggilan tidak bisa masuk.
Putar nomor 166 - Info Telkom Memo atau
Klik http://www.plasa.com/informasi/telkom-memo/

---------------------------------------------------------------------------