Reading a certain bit from a u32int

OmarShagie3

I input a value containing an unsigned 32-bit int and I want to read bit number 19. How can I do that?

This is the input I want to read the bit from:

uint32 Input = u32AHI_DioReadInput();
int x = ??? 

I tried using Input(19) and input[19] but i didn't work cause the first one is a function and the second is considered as if it is an array. I know that I should and some of the bits and shift them but I just don't know how can I do that.

Sergey Kalinichenko

When you need to read a single bit n, shift the number right by n positions, and cut out all bits except the last one, like this:

int x = (Input >> n) & 1;

The >> n shifts the content by n bits to the right; the & 1 does a bitwise "AND" with one (its binary representation has only the lowest bit set to one, all other bits contain zeros);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

reading 32bit variables from char buffer

From Dev

MATLAB reading bit by bit from a binary file

From Dev

C++ Binary Writing/Reading on 32bit to/from 64bit

From Dev

C++ Binary Writing/Reading on 32bit to/from 64bit

From Dev

Reading from a textfile in a certain format

From Dev

C++ 32 bit integer from file to 8 bit character to file - program crashing at certain integer

From Dev

Bit fields for reading from H/W registers

From Dev

Reading from a certain line onwards in python

From Dev

Reading/editing a file from a certain point

From Dev

32 bit pyodbc reading 64 bit access (accdb)

From Dev

Reading a 32bit dll file in IDL 64bit

From Dev

Reading the value of 31st bit of a 32bit integer

From Dev

Unsigned int from 32 bit to 64bit OS

From Dev

Migrating from 64 bit to 32 bit

From Dev

Byte conversion from 32 bit to 8 bit

From Dev

moving from ubuntu 32 bit to 64 bit

From Dev

going from 32 bit to 64 bit in assembly

From Dev

moving from ubuntu 32 bit to 64 bit

From Dev

Reading 24-bit samples from a .WAV file

From Dev

reading unsigned integer 64 bit from binary file

From Dev

Reading variables from array of byte when positioned with arbitrary bit length

From Dev

Strange values when reading pixels from 24-bit bitmap

From Dev

Reading 16-bit integers from binary file c++

From Dev

Python - reading 10 bit integers from a binary file

From Dev

Invalid cast exception from SqlDataReader when reading bit

From Dev

reading uint16_t from bit stream

From Dev

reading unsigned integer 64 bit from binary file

From Dev

Reading from a certain line until there is no more content in Java

From Dev

reading in certain rows from a csv file using python

Related Related

HotTag

Archive