စံကား

05Operators

Bitwise operators

Work on the individual bits of an integer.

Inside the computer, an integer is a row of bits, each 0 or 1. The number 202 in 8 bits is 11001010. Bitwise operators work on these bits one position at a time. Beginners seldom need them. These are useful for flags, colours, and low-level data. Feel free to skip this page and come back later.

စံကား

Expected output

၁၃၈
၂၃၄
၉၆
၅၃
၄၀
၁၂
၁၃၈
၂၃၄
၉၆
၅၃
၄၀
၁၂
SignWordsReads asEach result bit is 1 when
a & ba နှင့် b ၏ ဘစ်အလိုက် ဆုံလဒ်"bitwise meeting"both bits are 1
a | ba နှင့် b ၏ ဘစ်အလိုက် ပေါင်းစုလဒ်"bitwise union"either bit is 1
a ^ ba နှင့် b ၏ ဘစ်အလိုက် ခြားနားလဒ်"bitwise difference"the bits differ
~aa ၏ ဘစ်အလိုက် ပြောင်းပြန်ပြောင်းလဒ်"bitwise flip"the bit was 0
a << na ကို ဘယ်သို့ n ဘစ် ရွေ့သောရလဒ်"a moved n bits to the left"(bits move left)
a >> na ကို ညာသို့ n ဘစ် ရွေ့သောရလဒ်"a moved n bits to the right"(bits move right)

ဘစ်အလိုက် means "bit after bit". ဘယ်သို့ is "to the left", and ညာသို့ is "to the right".

Rules

  • Both sides must be integers.
  • A shift must move fewer places than the integer has bits:
စံကားZK0407

Expected error

error[ZK0407]: shift distance 8 is not below the 8-bit width

A practical use

A mask keeps some bits and clears the rest. & F0 (11110000) keeps the top four bits:

စံကား

Expected output

၁၉၂

Hands on

  • ၁ << n is 2 to the power n. Print 2⁵ with a shift.
  • Print whether bit 0 of 202 is set: (က & ၁) != ၀.

Next: Converting between kinds.