# 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 1●3●3●4 accept rate: 0% multipleinte... 1.3k●15●23●40

 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 3.1k●4●20●41 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 202●4●7●14 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 1●3●3●4 accept rate: 0%
 toggle preview community wiki:

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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

### Related questions

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