EXAMPLE - Bitwise operation

Concept

It explains how the LoLe basic bitwise operation is.

LoLe has two operations: Array operation and bit operation. 

These operations are done by subscript operator (square brackets []).

Example

import LoLe package

import Mem operation which is used for memory buffer.

from LoLe.Mem import Mem32

Using Mem(0x20) to create 0x20 bytes buffer.

In this code, TestMem is an array, each element holds 32bit data.

It uses malloc to arrange system memory.

TestMem = Mem32 (0x20)

subscript operaton

Using subscript operator to denote any data in TestMem.

The value in subscript operator after TestMem can be any value from 0 to 0x1F.

In this code

TestWord = TestMem[0x04]

Any TestWord operation effects the 4th to the 7th bytes in TestMem. other data in TestMem remain the same.

TestMem[0x04] is the same as TestMem[0x04:0x07].

Write bits

the TestWord[3] = 1 implementation concept is denote in this pseudo code.

(1 << 3) | (TestWird & (~(1 << 3)))

the TestWord[3] = 0 implementation concept is denote in this pseudo code.

(0 << 3) | (TestWird & (~(1 << 3)))

Read bits

the TestWord[3] implementation concept is denote in this pseudo code.

(TestWord >> 3) & 0x01

So in this statement

print (TestWord[3]) 

It outputs the 3rd bit of TestWord to standard output.

To output whole data in TestWord, the statement is

print (TestWord)

Source code

bitwise.py

 

Comments

Popular posts from this blog

How to extract bits from interger in python

Waht is LoLe?