LESSON 14 OF 52 · CHAPTER 01

Bitwise Fields

Hex/binary integer literals, |=, &, >>>, << and ~

ORIGINAL SOURCE

14-bitwise-fields.srp

// Pack and inspect a 64-bit word without implicit numeric conversions.
var flags = 0xA0;
flags |= 0b0011;
println(flags & 0x0F);
println(flags >>> 4);
println(1 << 63);
println(~0);
EXPECTED OUTPUT

stdout

Verified
3
10
-9223372036854775808
-1

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Hex/binary integer literals, |=, &, >>>, << and ~

Expected behavior

3, 10, signed 64-bit minimum, -1

Practice: Read the source before looking at the output. Predict the values and their order, then use the pinned compiler to execute this lesson and compare the exact fixture. Identify which operation produces the result.

बिटवाइज़ ऑपरेटर

बिटवाइज़ ऑपरेटर पूर्णांक की बाइनरी बिट्स पर काम करते हैं। 0xA0 हेक्स और 0b0011 बाइनरी मान हैं। |= बिट्स जोड़ने जैसा OR असाइनमेंट है; & 0x0F निचली चार बिट्स चुनता है; >>> 4 दाहिने शिफ्ट करता है। ये बूलियन and और or से अलग हैं।

आउटपुट: 3, 10, -9223372036854775808, -1। इस उदाहरण में 1 << 63 से signed 64-bit न्यूनतम मान दिखाई देता है और ~0 से सभी बिट्स उलटने पर -1 आता है।

अभ्यास: 0xA0 | 0b0011 के बाद flags का हेक्स मान क्या होगा? & 0x0F लगाने पर 3 क्यों मिलेगा?

Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.

TRY IT LOCALLY

Verify, then execute

Use the pinned compiler from the course root. Keep any imported _support files next to the source.

scarp verify examples/01-basics/14-bitwise-fields.srp scarp exec examples/01-basics/14-bitwise-fields.srp

The website does not run code. Read the CLI guide ↗

Pinned compiler: Scarp v0.0.6-alpha.3 · ceff07d9014f. No simulated output.