Announcement

Collapse
No announcement yet.

Simple question: how to use && for binary numbers ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple question: how to use && for binary numbers ?

    I use this but it seems not to work correctly.
    To have shorter serial command, I use a byte type coding function . I need now extracting every value: bit0=system on, bit1=present id card, bit 3=alarm ... and so on.
    Actually, Alim give me 1 even if code is 00000010 (it should be zero of course!)
    I suppose I don't write correctly.

    Can you help me,
    Thanks


    func DecoderFonctions1(var code)
    //récupère la valeur des bits du champ 2 (troisième champ)
    Fonction1:=code; // pour controle test
    Alim :=code && 0b00000001;
    Carte :=code && 0b00000010;
    Carte :=Carte >> 1;
    Alarme :=code && 0b00000100;
    Alarme :=Alarme >> 2;
    Feux :=code && 0b00001000;
    Feux :=Feux >> 3;
    Frein :=code && 0b00010000;
    Feux :=Frein >> 4;
    Clignot :=code && 0b00100000;
    Clignot :=Clignot >> 5;
    DatasInvalides:=code&&0b01000000;
    DatasInvalides:=DatasInvalides >> 6;

    endfunc

  • #2


    I think my question could be: how can I do logical binary operations ? what is the syntax for and operation ?

    Is it like this (for making AND between two values):

    result:= 12345678 && 0b00000001 ; // -> result=8 ???

    No, something's wrong

    I tried to do this:

    0b00000010 & 0B11111111 works for selecting a bit.
    The problem is if I replace one of those binary numbers by "code". I think this is a word space problem (16 bits instead 8 or something like that)

    I found a solution. Doesn't understand why it does'nt works correctly with other one.

    func DecoderFonctions1(var code)
    //récupère la valeur des bits du champ 2 (troisième champ)

    Alim :=code & 0b00000001;
    code:=code >> 1;
    Carte :=code & 0b00000001;
    code:=code >> 1;
    Alarme :=code & 0b00000001;
    code:=code >> 1;
    Feux :=code & 0b00000001;
    code:=code >> 1;
    Frein :=code & 0b00000001;
    code:=code >> 1;
    Clignot :=code & 0b00000001;
    code:=code >> 1;
    DatasInvalides:= code & 0b00000001;


    endfunc


    If I write code >> 4 that doesn't work so I only do one bit shift now.

    Comment

    Working...
    X