How to check the value of a specific bit in a number expressed in hexadecimal format?
For example, if the number is represented in hexadecimal as “0x60”, in binary it would be “01100000”. How can we determine the value of, let’s say, the 2nd or 7th bit? Are there any built-in functions for this in MikroTik?
I tried to do the following:
:local value 0x60;
:local bit0Value ( $value & 0x1 > 0 );
:put "The value of the 0 bit.: $bit0Value";
:local bit1Value ( $value & 0x2 > 0 );
:put "The value of the 1 bit.: $bit1Value";
:local bit2Value ( $value & 0x3 > 0 );
:put "The value of the 2 bit.: $bit2Value";
:local bit3Value ( $value & 0x4 > 0 );
:put "The value of the 3 bit.: $bit3Value";
:local bit4Value ( $value & 0x5 > 0 );
:put "The value of the 4 bit.: $bit4Value";
:local bit5Value ( $value & 0x6 > 0 );
:put "The value of the 5 bit.: $bit5Value";
:local bit6Value ( $value & 0x7 > 0 );
:put "The value of the 6 bit.: $bit6Value";
:local bit7Value ( $value & 0x8 > 0 );
:put "The value of the 7 bit.: $bit7Value";
/system/script> run test
The value of the 0 bit.: false
The value of the 1 bit.: false
The value of the 2 bit.: false
The value of the 3 bit.: false
The value of the 4 bit.: false
The value of the 5 bit.: true
The value of the 6 bit.: true
The value of the 7 bit.: false
0x60 = 0110 0000, so 2 bits will return true.
And you need to test with moving bit, so
0x1, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
:local value 0x60;
:local bit0Value ( $value & 0x1 > 0 );
:put "The value of the 0 bit.: $bit0Value";
:local bit1Value ( $value & 0x2 > 0 );
:put "The value of the 1 bit.: $bit1Value";
:local bit2Value ( $value & 0x4 > 0 );
:put "The value of the 2 bit.: $bit2Value";
:local bit3Value ( $value & 0x8 > 0 );
:put "The value of the 3 bit.: $bit3Value";
:local bit4Value ( $value & 0x10 > 0 );
:put "The value of the 4 bit.: $bit4Value";
:local bit5Value ( $value & 0x20 > 0 );
:put "The value of the 5 bit.: $bit5Value";
:local bit6Value ( $value & 0x40> 0 );
:put "The value of the 6 bit.: $bit6Value";
:local bit7Value ( $value & 0x80 > 0 );
:put "The value of the 7 bit.: $bit7Value";
I tried to do this and got the wrong result. But apparently my function of converting hex to bit was wrong, because I got it 10010110
I also tried version 0x1, 0x02, 0x04, 0x08, 0x16, 0x32, 0x64, 0x128. Apparently I do not quite understand how to properly convert to another type of display.
Tell me more. Does Mikrotik have a built-in function that would immediately print out the converted number from hex to bit?
I learned about this for about a week))) I haven’t fully mastered it yet))) And look at the request that I added above. While correcting the answer, you have already written your own.
Obtain a bit value from a byte (expected input from 0x00 to 0xFF, and the wanted bits. If the bits number is not specified, convert all byte to bit.
0x can be omitted
As wanted bit(s) can be specified one or more position.
Position is from 8 to 1.
> :put [$getMyBitFromHex “0xA8”]
10101000
> :put [$getMyBitFromHex “0xA8” 8]
1
> :put [$getMyBitFromHex “0xA8” 765]
010 <<== is 2
> :put [$getMyBitFromHex “0xA8” 4321]
1000 <<== is 8
no matter the specified bits order, or if is specified a wrong position or same time the same position.
One moment, do not do confusion…
\xx is supposed to be a hexadecimal number, not a decimal number, do not call the var “num”, call it “hex”
you continuosly confuse the number base…
0x41 is 65, so that way can not be used to print decimal “41” as character.
41 on hex is 0x29 and the correct character is the )
If you really want convert HEXADECIMAL number 0x41 to a character “A”: