Bit ...

Discussion in 'Teaching Programming with B4X' started by ilan, Apr 2, 2017.

  1. ilan

    ilan Expert Licensed User


    Can someone please explain what does bit.shiftlet, bit.and, bit.or,..

    How can we use it and in what cases do we use them.

    The reason i ask is i try to learn Spritekit and @JanPRO have provided 2 examples. (Flappy bird and brick)

    Now in flappy bird i see that he is declaring the collisions categories as int so you can set what body will collide with what category but he is doing it like this:

    Private category_bird as int
    Private category_world as int
    Category_bird = bit.shiftleft(1,0)
    Category_world = 
    Why using bit.shiftleft and not give it a number like 1,2,3,....?

    I dust dont understand the use of bit.x can someone please explain.

    Thank you :)
    Last edited: Apr 2, 2017
  2. stevel05

    stevel05 Expert Licensed User

    ilan likes this.
  3. ilan

    ilan Expert Licensed User

    Ok thanks @stevel05

    But i still have a question

    Bit.shiftleft(1,0) will return 1
    Bit.shiftleft(1,1) will return 2
    Bit.shiftlwft(1,2) will return 4

    So why not just write

    Category_bird = 1

    Is there any benefit in using bit instead of writing the number you want?
    Last edited: Apr 3, 2017
  4. stevel05

    stevel05 Expert Licensed User

    You can store more than 1 value in the int, in the case of booleans you can use each bit as a separate flag, so rather than using 32 boolean variables as flags, you could store them all in 1 int and access then with the Bit operator.
    fireday, inakigarm and ilan like this.
  5. ilan

    ilan Expert Licensed User

    Thank you. I still need to understand where and how it can be used and where it is a better choice then an int and yours answers are a very good start for that :)
  6. canalrun

    canalrun Well-Known Member Licensed User

    I think that bit operations were more useful in the olden days before there was tons of memory and powerful CPUs. I think it's best to think in terms of binary when using bit operations. When math operations were slow it used to be handy to use a right shift by one bit to divide by two and a left shift by one bit to multiply by two. You can also accomplish non-powers of two by shifting and adding back the original number in various combinations.

    Another useful use for binary numbers and shift operations was implementing a state machine – especially long before there were CPUs. You could just have a shift register and use the resulting bit pattern to control your state. Think of a traffic light. If you just shift the bit in a register by one position every 15 seconds and use the resulting bit pattern to control the red, yellow, or green light, you have a very simple state machine.

    When looking at the Android documentation for certain functions (camera parameters for example), they use values to represent parameters that are essentially 1's in certain bit positions of an integer.

    From the documentation:
    ShiftLeft (N As Int, Shift As Int) As Int
    Shifts N left.
    Shift - Number of positions to shift.
    ShiftRight (N As Int, Shift As Int) As Int
    Shifts N right.
    Keeps the original value sign
    Shift - Number of positions to shift.

    I'm not familiar with the code that is referenced, but he may be using shift operations for some kind of implementation of a "state" machine.

    I personally think it's easier and more intuitive to use full-length Boolean, integer, or enum variables to accomplish this.

    fireday and ilan like this.
  7. freedom2000

    freedom2000 Well-Known Member Licensed User

    Bit operators are still commonly used with Microcontrolers where memory is not big...
    One 8 bits words = 8 states of 8 buttons for example
    fireday, lemonisdead and JordiCP like this.
  8. sorex

    sorex Expert Licensed User

    For some people it easier to roll a bit a few times instead of using the right value. most programmers know that these are 2^n variants tho and know them out of their head.
  9. wonder

    wonder Well-Known Member Licensed User

    @ilan, for example bitmap operations:
    unsigned long createRGB(int r, int g, int b)
    return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);

    Input: createRGB(162, 30, 164)
    Output: 0xA21EA4
    ilan, inakigarm and Cableguy like this.
  10. melonZgz

    melonZgz Active Member Licensed User

    I use a lot of bitwise operators when programming microcontrollers. You have to, to configure registers, to read inputs, to manage outputs, to store booleans (in one byte you can store 8 booleans)... but I think I've never used any of them with b4x...
  11. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Bit shift left and bit shift right is still used a lot within industrial control systems. A very simple example would be part tracking on a conveyor where the shift occurs with every pulse of the encoder and a 1 is set when a part is loaded. The 1 then shifts with each pulse of the encoder until the desired bit position which might trigger a reject sequence or the next operation.
    Bit shifting is also good for storing or buffering parts. If a process is unable to accept a component you might want to buffer it i.e. bit shift left a value of 1 and continue doing this as each new part arrives. Once the process is ready to accept parts again you simply bit shift right and unload all the 1's until there are no more.
    Maybe you have heard of terms such as FIFO and FILO, first-in-first-out and first-in-last-out? These were typically associated with bit shift operations.
  12. sorex

    sorex Expert Licensed User

    if you want to try some retro coding (c64, speccy whatever) then prepare for a lot of that stuff. :)

    memory was limited so for example all gfx data (bitmaps, charsets, sprites) are based on bit settings inside bytes.
    for example 8 hires pixels is 1 byte (1 bit/pixel). 4 multi color pixels is also 1 byte (2 bits/pixel).

    so people who coded back then still think like that and it's a good way to optimize game data aswell.
    wonder likes this.