Solved

how to read a bit from a byte

Posted on 2002-04-24
12
227 Views
Last Modified: 2010-05-02
Can you please provide me with a code to read a specific bit of a byte
0
Comment
Question by:yahya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6965427
dim bytValue as byte
dim bytCheck as byte

bytValue = 10  '1010
bytCheck = 2   '0010

bytCheck and bytValue -> returns true

bytValue = 8  '1000
bytCheck = 2  '0010

bytCheck and bytValue -> returns false

Hope this gives you the idea
CHeers
0
 

Author Comment

by:yahya
ID: 6965434
that is very helpfull,
What I exactly need is to retrieve a specific bit position not specific value. byte has 8bits and i want to read for instance the 2nd or third or any position.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 6965439
if you want to determine if a specific bit is set (1) or not (0) then you use the AND operator, with a number which has the value of 2 raised to then power of the Bit Number (for bit 0, -the First bit is always refered to as Bit 0- you would use 1 (= 2^0), for bit 1,you would use 2 and so on:

Dim Bit0Value as Integer
Dim Bit1Value as Integer
Dim Bit2Value as Integer
Dim Bit3Value as Integer
Dim Bit4Value as Integer
Dim Bit5Value as Integer
Dim Bit6Value as Integer
Dim Bit7Value as Integer

Bit0Value = IIF(MyByteValue AND 1 = 1, 1, 0)
Bit1Value = IIF(MyByteValue AND 2 = 1, 1, 0)
Bit3Value = IIF(MyByteValue AND 4 = 1, 1, 0)
Bit4Value = IIF(MyByteValue AND 8 = 1, 1, 0)
Bit5Value = IIF(MyByteValue AND 16 = 1, 1, 0)
Bit6Value = IIF(MyByteValue AND 32 = 1, 1, 0)
Bit7Value = IIF(MyByteValue AND 64 = 1, 1, 0)
Bit8Value = IIF(MyByteValue AND 128 = 1, 1, 0)

this will return 1 if the bt is ON, and 0 if the bit is off, for each of the 8 bits in the Byte value.

Arthur Wood




0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:yahya
ID: 6965454
What if you have two bytes and i want to read the last two bits of the first and the first two of the second. Is there anything special.
0
 
LVL 2

Expert Comment

by:GregJennings
ID: 6965622
Arthur's suggestion can be used to do that.  However, if you always want just the last two bits of the first byte and the first two bits of the last byte, you can do this:

Value = ((FirstByte AND (2+1)) * 4) + (LastByte AND (128+64))

This will give you a single value from the 4 bits you mentioned.  Is this what you're looking for?
0
 
LVL 2

Expert Comment

by:GregJennings
ID: 6965623
Oops, forgot something...here's the right code.

Value = ((FirstByte AND (2+1)) * 4) + ((LastByte AND (128+64)) / 64)

0
 

Author Comment

by:yahya
ID: 6965643
thanks very much for helping.
The situation now is. I have three variables: v1 and v2 and v3
v1 will take the value of the the 2-7 bits of the first byte while v2 will be  0-1 of byte 1 and 0-7 of byte two which means v2 is more than a byte and v3 will hold byte3 0-7.

I know I need to define a construct of three variables
two to hold a byte value and one to hold a word which is v2.

can you please help in the syntax of writing this

many thanx
0
 
LVL 2

Expert Comment

by:GregJennings
ID: 6965702
yahya, you've only graded 4 of your last 14 questions.  Will you be sure to give the 250 points for this question?

Usually, bit 7 is the left-most bit of a byte, but by your question, it seems you're calling bit 0 the left-most bit.  So, my answer assumes you're calling the left-most bit 0 and the right-most bit 7.  Please let me know if you're calling the left-most bit 7 and the right-most bit 0.

Dim v1 As Integer
Dim v2 As Integer
Dim v3 As Integer

v1 = (FirstByte And (32+16+8+4+2+1))
v2 = ((FirstByte And (128+64)) * 4) + (SecondByte)
v3 = ThirdByte
0
 
LVL 2

Expert Comment

by:GregJennings
ID: 6965712
If the left-most bit is 7 and right-most bit is 0...

v1 = (FirstByte And (128+64+32+16+8+4)) / 4
v2 = ((FirstByte And (2+1)) * 256) + SecondByte
v3 = ThirdByte
0
 

Author Comment

by:yahya
ID: 6965727
Greg,

Can you explain to me please why are you dividing by 4 for v1 and * by 256 for v2 and the points are yours
0
 
LVL 2

Accepted Solution

by:
GregJennings earned 250 total points
ID: 6965804
Dividing by 4...
Since you're getting the top 6 bits (bits 2-7) of the byte, you need to "shift" those 6 bits down toward bit 0 to get the value of those bits.  To shift down by 1 bit, divide by 2.  To shift down by 2 bits, divide by 4.  To shift down by 3 bits, divide by 8.  In general, to shift down by X bits, divide by (2 to the X power).

Multiplying by 256...
You want 2 bits (bits 0-1) from the first byte to become bits 8-9 for the new value, v2.  So, to shift bits 0-1 up to the bits 8-9 position, you need to multiply by 256.  In general, to shift up by X bits, multiply by (2 to the X power).
0
 
LVL 2

Expert Comment

by:GregJennings
ID: 6965826
Thanks yahha.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question