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

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


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.

permanent link

answered 15 Jul '11, 14:06

helloworld's gravatar image

helloworld
3.1k42041
accept rate: 28%

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.

permanent link

answered 15 Jul '11, 06:25

izopizo's gravatar image

izopizo
2024714
accept rate: 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

permanent link

answered 15 Jul '11, 12:25

Pfanne's gravatar image

Pfanne
1334
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×431
×1

question asked: 15 Jul '11, 05:56

question was seen: 29,247 times

last updated: 15 Jul '11, 14:06

p​o​w​e​r​e​d by O​S​Q​A