This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

accessing bits from byte

I want access bit from Byte array.for example consider array of 30 bytes in which I want to access 1 to 15 bits...
How to access it. kindly help me.......

Parents
  • Did you look at my code?

    You divide by 8. Unless the compiler do a strength reduction, the division is very heavy for most embedded processors.

    You do modulo 8 (another division) to get the bit position inside the byte.

    You have a function called AccessBit() - which seem to imply that it accesses a single bit. But in this case it takes a start address and an end address. So what is the function intended to do?

    Did you compile with warnings on?

    You send in a parameter startAddr, but you do an assign startByte = startByte/8. Where do you give startByte an initial value before using it on the right-hand side in an expression? This implies that you are posting asking for help before looking for bugs in your code.

    If your start and end addresses points into the same byte, then you do:

    if(i==startByte&i==endByte)
    


    Did you intend a bit and (&) instead of a logic and (&&)? And what value do you assign to count when you do:

    count=endBit;
    


    In what way does endBit represent a count? Shouldn't startBit also been taken into account when you figure out the number of bits inside the range of the byte?

    And if startByte and endByte are the same - why have this test inside a for loop?

    No use trying to look for more errors in the code, since you haven't documented what the intended action should be - I can only look for bugs when I compare between a map and the reality. If I can't see the reality, I will not know if the map is good or bad.

Reply
  • Did you look at my code?

    You divide by 8. Unless the compiler do a strength reduction, the division is very heavy for most embedded processors.

    You do modulo 8 (another division) to get the bit position inside the byte.

    You have a function called AccessBit() - which seem to imply that it accesses a single bit. But in this case it takes a start address and an end address. So what is the function intended to do?

    Did you compile with warnings on?

    You send in a parameter startAddr, but you do an assign startByte = startByte/8. Where do you give startByte an initial value before using it on the right-hand side in an expression? This implies that you are posting asking for help before looking for bugs in your code.

    If your start and end addresses points into the same byte, then you do:

    if(i==startByte&i==endByte)
    


    Did you intend a bit and (&) instead of a logic and (&&)? And what value do you assign to count when you do:

    count=endBit;
    


    In what way does endBit represent a count? Shouldn't startBit also been taken into account when you figure out the number of bits inside the range of the byte?

    And if startByte and endByte are the same - why have this test inside a for loop?

    No use trying to look for more errors in the code, since you haven't documented what the intended action should be - I can only look for bugs when I compare between a map and the reality. If I can't see the reality, I will not know if the map is good or bad.

Children
No data