0
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.

4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by WaltP
1

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;

.

0

>>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 :)

1
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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.