Read 'N' bit from a byte

read meaning
read and read
(read ) usage
read synonym
read past
how to pronounce read
i read a book
to read in spanish

I need to read a specific bit from a byte. The value i want to test is 0 or 1.

unsigned char Buffer[0]=2; 
//or binary 0b00000010

How can i read n bit from buffer. If it's 0 or 1? Example if 7 bit from byte is 0 or 1

Read, synonyms: peruse, study, scrutinize, look through, pore over, devour, be absorbed in, bury oneself in, wade through, plow through, run one's eye over, cast an eye over, leaf through, scan, glance through, flick through, skim through, thumb through, flip through, browse through, dip into, con, decipher, make out, make sense of, interpret, understand, comprehend read: [verb] to receive or take in the sense of (letters, symbols, etc.) especially by sight or touch. to study the movements of with mental formulation of the communication expressed. to utter aloud the printed or written words of. to learn from what one has seen or found in writing or printing. to become acquainted with or look over the

To check a bit if its 0 or 1, you can define a simple macro like:

#define BIT_ISSET(var, pos) (!!((var) & (1ULL<<(pos))))

and then use it in if-clauses.

Note the '!!' operator, to ensure that it returns 0 or 1.

read, discover (information) by reading it in a written or printed source. Define read. read synonyms, read pronunciation, read translation, English dictionary definition of read. reproduce written words mentally or utter them aloud: read a

To answer your question: which will check the value of 7th bit.

unsigned char Buffer = 2; //Hope this is what you are looking for 

if (((Buffer >> 7) & 0x01) == 1 )
    printf(" Bit is 1 ");
    printf(" Bit is 0 ");

Similar way if you need to check the value of nth bit, replace 7 in if condition with n.

READ, "he didn't dare look away, in case this was read as a sign of weakness" verb (used with object), read [red] /rɛd/, read·ing [ ree -ding] /ˈri dɪŋ/. to look at carefully so as to understand the meaning of (something written, printed, etc.): to read a book; to read music.

Of course you had to define your bit position in advance.

e.g. Bit7 (MSB) Bit0 (LSB)

Test for bit 7:

if (Buffer[0] & 0x80)
    //do action for bit 7 = 1
    // do action for bit7 = 0

read(2) - Linux manual page, synonyms: interpret, take, take to mean, construe, see, explain, understand read (third-person singular simple present reads, present participle reading, simple past read, past participle read or (archaic, dialectal) readen) A painting of a girl reading (transitive or intransitive) To look at and interpret letters or other information that is written. have you read this book?;  he doesn’t like to read

You can just use this function to pass your char and N(number of bit).The function will invoke a bitwise AND filtering out the bit you need to check.

bool check_N_bit(char a_char , int N)
    if (N>8)
        return false;
    if ((a_char&(0x01 << N-1)) > 0)
        return true;
        return false;

Read, inspect and record the figure indicated on (a measuring instrument). Some children can read (= have learned the skill of reading) by the age of four. to say the words that are printed or written: She read (the poem) slowly and quietly. [ + two objects ] Their teacher always reads them a story at the end of the day.

read, hear and understand the words of (someone speaking on a radio transmitter). read () attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read.

Read, to utter aloud or render in speech (something written, printed, etc.): reading a story to his children; The actor read his lines in a booming voice. to have such  Kindle Cloud Reader lets you read ebooks instantly in your web browser - no Kindle device required.

READ (verb) definition and synonyms, read (third-person singular simple present reads, present participle reading, simple past read, past participle read or (archaic, dialectal) readen). A painting of a  Reading Eggs makes learning to read interesting and engaging for kids, with great online reading games and activities. And it really works! Children love the games, songs, golden eggs and other rewards which, along with feeling proud of their reading, really motivate children to keep exploring and learning. Your child can learn to read right now.

  • To begin with, you want to create an array of zero elements, and store a value in it? That won't work very well.
  • char Buffer[0] = 2; is ill-formed. It assigns 2 to an array of length 0. (I believe, even arrays of 0 length are ill-formed.)
  • As for your problem, you can't really "read" bits. You can mask out the bits you want from a byte using bitwise AND.
  • In general, to test for individual bits, C has bitwise operators. Example: SO: C/C++ check if one bit is set in, i.e. int variable.
  • @Scheff Yes, arrays of size 0 are illegal. Its one of the classic ways to cause a compile-time error intentionally.
  • Thank you very much for the example i try this but it doesn't show that the number is 1. int bit7 = ((unsigned char)Buffer[0] >> 7) & 1; if(bit7==1) //rest of code
  • @ddd Yes it does. What exactly is the problem?
  • @ddd Check with e.g.: #include <stdio.h> int main() { // bit 7: | | | | | // binary: 0000 0000 1000 0000 0111 0000 1010 0000 0111 1001 unsigned char buffer [] = {0x0, 0x80, 0x70, 0xa0, 0x79}; for(size_t i = 0; sizeof(buffer) > i; ++i) { printf("%u %i\n", buffer[i], ((unsigned char)buffer[i] >> 7) &1); } } . It does what you asked for, doesnt it?
  • @ddd I think you are numbering the 1 in your example 0b00000010 as bit 7, which is not a convention anyone will recognize. With this notation, one usually starts counting at 0 from right to left. And if you want to extract that bit, you do ((unsigned char)Buffer[0] >> 1) & 1
  • I don't know why but it doesnt returns me 1:(
  • Bit count 0 ...7
  • But I guess that is not the problem
  • I think ddd and Swordfish are both confused about the conventions for numbering bits. FYI numbering starts at zero from the right. The number 2 has bit 1 set, not bit 7. Bit 7 is equivalent to 128.
  • it seems i am counting down from 42. nevermind, i was talking bs ;)