105(decimal) = 01101001(binary)

for example, i want to skip first 3 bits of binary and get the number to the right and assign it to an integer:

01001(binary) = 9 (decimal)

could you plz give a little code sample?

thanx.

Hi,

Its simple,
1. Just use the shift operator. First n times left shift and then again n times right shift. This will make all zeroes in beginning.

int sourceNo=105, destNo;
int n;
destNo = sourceNo<<n; // Here n is the number of bits  you want to skip.

destNo = destNo >> n;

2. the number of bits you want to skip, just do a Bitwise AND with that number of Zeroes. Please find the code below.

int souceNo=105, destNo;
int andMask = ~0;
andMask = andMask >>n; // Here n is the number of bits  you want to skip.

destNo = sourceNo & andMask;

.

>>destNo = sourceNo<<n; // Here n is the number of bits you want to skip.

Here, the value of n is undefined because you didn't initialize it to anything in the previous line :)

105(decimal) = 01101001(binary)

for example, i want to skip first 3 bits of binary and get the number to the right and assign it to an integer:

01001(binary) = 9 (decimal)

could you plz give a little code sample?

thanx.

Set a mask to clear the first 3 bits:

int mask = 0x1F;    // binary 00011111
newint = oldint & mask;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.