Split integer in 2 part

11 views (last 30 days)
Domenico Bellantoni
Domenico Bellantoni on 10 Jun 2015
Answered: Image Analyst on 10 Jun 2015
Hi, I am new in `MATLAB` and what I would like to do is this kind of thing: I have a number lets say that it can be represented with `24 bits` so lets take the number `14057474` for example, which in binary is `000101010111001100110011`. I want to retreive the last 15 bits `111001100110011` in order to get the number `29491`. So basically what I want to do is to split a unsigned integer number in 2 part: the less significant bits 'l' and the most significat bits 24-l and then retreive the decimal number. How can I do this?
  1 Comment
Image Analyst
Image Analyst on 10 Jun 2015
Your number is wrong:
n = 14057474
str = dec2bin(n)
str =
110101101000000000000010

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 10 Jun 2015
Try bitand():
n = 14057474
str1 = dec2bin(n) % Show it in binary.
numRightBits = 15;
out = bitand(n, 2^numRightBits - 1)
str2 = dec2bin(out) % Show it in binary.
n = 1405747
str3 = dec2bin(n) % Show it in binary.
numRightBits = 15;
out = bitand(n, 2^numRightBits - 1)
str4 = dec2bin(out) % Show it in binary.
In the command window:
n =
14057474
str1 =
110101101000000000000010
out =
2
str2 =
10
n = 1405747
str3 =
101010111001100110011
out =
29491
str4 =
111001100110011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!