binary - How to look at a certain bit in C programming? -
i'm having trouble trying find function @ bit. if, example, had binary number of 1111 1111 1111 1011, , wanted @ significant bit ( bit way left, in case 1) function use @ bit?
the program test if binary number positive or negative. started off using hex number 0x0005, , using two's compliment function make negative. now, need way check if first bit 1 or 0 , return value out of that. integer n equal 1 or 0 depending on if negative or positive. code follows:
#include <msp430.h> signed long x=0x0005; int y,i,n; void main(void) { y=~x; i=y+1; }
there 2 main ways have done in past. first bit mask use if checking exact same bit(s). example:
#define mask 0x80000000 // return value of "0" means bit wasn't set, "1" means bit was. // can check many bits want call. int applymask(int number) { return number & mask; }
second bit shift, mask (for getting arbitrary bit):
int checkbit(int number, int bitindex) { return number & (1 << bitindex); }
one or other of these should looking for. best of luck!
Comments
Post a Comment