0Pricing
Zig Academy · درس

أعداد صحيحة بعروض اعتباطية مثل u3

استخدم أنواع أعداد بعرض بتات دقيق

أعداد صحيحة بعروض اعتباطية مثل u3 درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Zig Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Zig Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Beyond u8 and u32

Zig is not limited to the usual sizes. You can ask for an integer of any bit width, from u1 all the way up to enormous values.

The uN Pattern

Write u followed by a number to get an unsigned integer of exactly that many bits. So u3 holds three bits.

const level: u3 = 5;

Signed Versions Too

Swap the u for an i to get a signed integer of any width, so i4 covers a small signed range.

const delta: i4 = -3;

Exact Range Control

A u3 stores values from 0 to 7, because three bits give eight combinations. The type itself enforces that range.

Find the Limits

Ask the type for its bounds with maxInt and minInt, instead of memorizing them yourself.

const hi = std.math.maxInt(u3); // 7
const lo = std.math.minInt(u3); // 0

Even One-Bit Integers

A u1 holds just 0 or 1, acting like a single raw bit you can store and compute with directly.

const bit: u1 = 1;

u0 Is a Real Type

Even u0 exists. It can only ever be the value 0 and takes up no bits, which is handy in generic code.

Perfect for Packed Fields

Odd widths like u3 and u5 are ideal inside a packed struct, letting each field claim exactly the bits it needs.

const Color = packed struct {
    r: u5,
    g: u6,
    b: u5,
};

Watch for Overflow

Assigning 8 to a u3 overflows its range. In safe modes Zig catches this at runtime instead of letting it wrap silently.

Cast to Widen

Use @as or @intCast to move a narrow value into a wider type when you need more room to compute.

const small: u3 = 5;
const big: u16 = @as(u16, small);

Express Intent

Arbitrary widths let the type system document your real constraints, so a u4 makes it clear a value never exceeds 15. 🎯

Quick Check

Think about how many distinct values fit in three bits.

Recap

You met arbitrary-width integers like u3 and i4: exact ranges, perfect for packed fields, with overflow checked in safe modes. 🎉

الأسئلة الشائعة

هل درس «أعداد صحيحة بعروض اعتباطية مثل u3» مجاني؟

نعم — نص درس «أعداد صحيحة بعروض اعتباطية مثل u3» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.

ماذا ستتعلم في «أعداد صحيحة بعروض اعتباطية مثل u3»؟

استخدم أنواع أعداد بعرض بتات دقيق تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟

لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «أعداد صحيحة بعروض اعتباطية مثل u3»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟

نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ‏packed struct للتخطيط الدقيق
  2. أعداد صحيحة بعروض اعتباطية مثل u3
  3. حقول البتات والعلامات
  4. ‏extern struct وتخطيط ABI
← العودة إلى Zig Academy