This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

How to checkout a Bit in a Byte?

0

Hi, just a short simple question:

How can i checkout a specific Bit in a Byte? I tryed something like this:

for i = Value_Start, Value_Stop do
  local c = buffer(i,1):uint()  <--- is this the right valuetype???

if c and 2^1 == 1 then ….. if c.1 == 1 then ….. if c(1) == 1 then ….. if c[1] == 1 then ….. if c:1 == 1 then ….. if c and 0x00000001 == 1 then ….. if c and 128 == 1 then ….. end

can anybody give me a short hint, please

Greets from Hamburg Pfanne

asked 15 Jul ‘11, 05:56

Pfanne's gravatar image

Pfanne
1334
accept rate: 0%

retagged 15 Jul ‘11, 07:23

multipleinterfaces's gravatar image

multipleinte…
1.3k152340


3 Answers:

2

TvbRange supports a bitfield function that extracts the specified number of bits from an offset.

Also, Wireshark Lua natively supports Lua BitOp (without downloading any external libraries). I recommend using the native support instead of your own Lua bitops functions.

The documentation from those links should be clear enough.

answered 15 Jul '11, 14:06

helloworld's gravatar image

helloworld
3.1k42041
accept rate: 28%

0

Lua doesn't support bit operations by default. You may need to download a library like the one from

http://luaforge.net/projects/bit/

Number of bitwise operations are supported

Also I'm not sure how to add a tag but this question should have been tagged with "lua" as well.

answered 15 Jul '11, 06:25

izopizo's gravatar image

izopizo
2024714
accept rate: 0%

0

Hi izopizo,

thanks for your hint. I have solved my problem like this.

-- Convert Byte to BitArray ####################################################
local function to_bits(n)
 --check_int(n)
 --if(n < 0) then
  -- negative
  --return to_bits(bit.bnot(math.abs(n)) + 1)
 --end

– to bits table local tbl = {} local cnt = 1 while (n > 0) do local last = math.mod(n,2) if(last == 1) then tbl[cnt] = 1 else tbl[cnt] = 0 end n = (n-last)/2 cnt = cnt + 1 end return tbl end

for i = Value_Start, Value_Stop do local c = buffer(i,1):uint()

local BitTable = to_bits(c)
if BitTable[8] == 1 then Bit8 = Bit8 .. " 1 " else Bit8 = Bit8 .. " . " end if BitTable[7] == 1 then Bit7 = Bit7 .. " 1 " else Bit7 = Bit7 .. " . " end if BitTable[6] == 1 then Bit6 = Bit6 .. " 1 " else Bit6 = Bit6 .. " . " end if BitTable[5] == 1 then Bit5 = Bit5 .. " 1 " else Bit5 = Bit5 .. " . " end if BitTable[4] == 1 then Bit4 = Bit4 .. " 1 " else Bit4 = Bit4 .. " . " end if BitTable[3] == 1 then Bit3 = Bit3 .. " 1 " else Bit3 = Bit3 .. " . " end if BitTable[2] == 1 then Bit2 = Bit2 .. " 1 " else Bit2 = Bit2 .. " . " end if BitTable[1] == 1 then Bit1 = Bit1 .. " 1 " else Bit1 = Bit1 .. " . " end end

I´am a little bid amezed, becouse Lua dosen´t support this comparatively simple operation.

Greets From Hamburg Pfanne

answered 15 Jul ‘11, 12:25

Pfanne's gravatar image

Pfanne
1334
accept rate: 0%