#import "../lib/styles.typ" : *
#import "../lib/common_rewrite.typ" : *
#import "@preview/cetz:0.4.2"

#set page(
  paper: "a4", 
  margin: (
    bottom: 10mm,
    top: 5mm,
    left: 5mm,
    right: 5mm
  ),
  flipped:true,
  numbering: "— 1 —",
  number-align: center
)

#set text(size: 8pt)

#place(top+center, scope: "parent", float: true, heading(
  [Computer Technik/Programmierpraktikum EI]
))

#let Allgemein = color.hsl(105.13deg, 92.13%, 75.1%)
#let colorProgramming = color.hsl(330.19deg, 100%, 68.43%)
#let colorNumberSystems = color.hsl(202.05deg, 92.13%, 75.1%)
// #let colorVR = color.hsl(280deg, 92.13%, 75.1%)
// #let colorAbbildungen = color.hsl(356.92deg, 92.13%, 75.1%)
// #let colorGruppen = color.hsl(34.87deg, 92.13%, 75.1%)


#let SeperatorLine = line(length: 100%, stroke: (paint: black, thickness: 0.3mm))
#let MathAlignLeft(e) = {
  align(left, block(e))
}
#columns(2, gutter: 2mm)[
  #bgBlock(fill: colorNumberSystems)[
    #subHeading(fill: colorNumberSystems)[ASCII Ranges]

    #table(
      columns: (1fr, 1fr, 1fr),
      [Range], [Hex], [Bits],
      [Upper Case], raw("0x41-0x5A"), [#raw("010XXXXX") (bit 6)],
      [Lower Case], raw("0x61-0x7A"), [#raw("011XXXXX") (bit 6)],
      [Numbers (0-9)], raw("0x30-0x39"), [#raw("0011XXXX")],
      [Ganz ASCII], raw("0x00-0x7F"), [#raw("0XXXXXXX")],
    )
  ]

  #bgBlock(fill: colorNumberSystems)[
    #subHeading(fill: colorNumberSystems)[Einer-Kompilment, Zweier-Kompliment, Float (IEEE 754)]

    *Float (IEEE 754)*

    #cetz.canvas({
      import cetz.draw : *
      let cell_size = 0.3;
      
      let manntise_stop = 22;
      let exponent_start = 23;
      let exponent_stop = 30;
      let sign_bit = 31;
      let total_bits = sign_bit + 1;


      for i in range(total_bits) {
        let bit = 31 - i;

        rect((i*cell_size, 0), (i*cell_size+cell_size, 0.5),
          fill: if bit == sign_bit { rgb("#8fff57") } else { 
            if ( bit >= exponent_start and bit <= exponent_stop) { rgb("#ffe057") } else { if (bit <= manntise_stop) {rgb("#57a5ff")} else { white } }
          },
          stroke: (thickness: 0.2mm)
        )
  
        content((i*cell_size + 0.5*cell_size, 0.25), raw(str(0)))
      }

      content((cell_size, 0.7), [sign], anchor: "east")
      content((5*cell_size, 0.7), [Exponent (#str(exponent_stop - exponent_start + 1) bit)])
      content((20*cell_size, 0.7), [Mantisse/Wert (#str(manntise_stop+1) bit)])

      rect((0,0), (32*cell_size, 0.5))

      content((cell_size*(total_bits - sign_bit), -0.2), anchor: "south", raw(str(sign_bit)), angle: 90deg)

      content((cell_size*(total_bits - exponent_stop), -0.2), anchor: "south", raw(str(exponent_stop)), angle: 90deg)

      content((cell_size*(total_bits - exponent_start), -0.2), anchor: "south", raw(str(exponent_start)), angle: 90deg)

      content((cell_size*(total_bits - manntise_stop), -0.2), anchor: "south", raw(str(manntise_stop)), angle: 90deg)

      content((cell_size*(total_bits), -0.2), anchor: "south", raw(str(0)), angle: 90deg)
    })

    #cetz.canvas({
      import cetz.draw : *
      let cell_size = 0.21;
      
      let manntise_stop = 51;
      let exponent_start = 52;
      let exponent_stop = 62;
      let sign_bit = 63;
      let total_bits = sign_bit + 1;


      for i in range(total_bits) {
        let bit = sign_bit - i;

        rect((i*cell_size, 0), (i*cell_size+cell_size, 0.5),
          fill: if bit == sign_bit { rgb("#8fff57") } else { 
            if ( bit >= exponent_start and bit <= exponent_stop) { rgb("#ffe057") } else { if (bit <= manntise_stop) {rgb("#57a5ff")} else { white } }
          },
          stroke: (thickness: 0.2mm)
        )
  
        content((i*cell_size + 0.5*cell_size, 0.25), raw(str(0)))
      }

      content((cell_size, 0.7), [sign], anchor: "east")
      content((7*cell_size, 0.7), [Exponent (#str(exponent_stop - exponent_start + 1) bit)])
      content((20*cell_size, 0.7), [Mantisse/Wert (#str(manntise_stop+1) bit)])

      rect((0,0), (total_bits*cell_size, 0.5))

      content((cell_size*(total_bits - sign_bit), -0.2), anchor: "south", raw(str(sign_bit)), angle: 90deg)

      content((cell_size*(total_bits - exponent_stop), -0.2), anchor: "south", raw(str(exponent_stop)), angle: 90deg)

      content((cell_size*(total_bits - exponent_start), -0.2), anchor: "south", raw(str(exponent_start)), angle: 90deg)

      content((cell_size*(total_bits - manntise_stop), -0.2), anchor: "south", raw(str(manntise_stop)), angle: 90deg)

      content((cell_size*(total_bits), -0.2), anchor: "south", raw(str(0)), angle: 90deg)
    })
  ]

  #bgBlock(fill: colorProgramming)[
    #subHeading(fill: colorProgramming)[C]

    #table(
      columns: (auto, 1fr),
      fill: white,
      raw("restrict", lang: "c"), [
        Funktions Argument modifier

        Gibt compiler den hint, das eine Pointer nur in der Funktion verwedent wird. Kann besser optimiert werden
      ],
      raw("volatile", lang: "c"), [
        Zwingt Compiler den Funktion/Variable nicht wegzuoptimieren
      ]
    )
  ]
]