加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在MATLAB中无符号整型转二进制

(2012-02-23 00:08:20)
标签:

it

分类: matlab
http://jaasyang.blog.163.com/blog/static/2662632201011610516277/
用到的函数为bitget,dec2bin。如果是二进制转十进制,那么就是bin2dec

bitget

Bit at specified position

Syntax

C = bitget(A, bit)

Description

C = bitget(A, bit) returns the value of the bit at position bit in A. Operand A must be an unsigned integer, a double, or an array containing unsigned integers, doubles or both. The bit input must be a number between 1 and the number of bits in the unsigned integer class of A (e.g., 32 for the uint32 class).

The output of bitget is independent of the endian settings of the computer you are using.

Examples

Example 1 — Binary Conversion

The dec2bin function converts decimal numbers to binary. However, you can also use the bitget function to show the binary representation of a decimal number. Just test successive bits from most to least significant:

disp(dec2bin(13))  1101    C = bitget(uint8(13), 4:-1:1)  C =       1     1     0     1

Example 2 — Binary Compare

Prove that intmax sets all the bits to 1:

a = intmax('uint8');  if all(bitget(a, 1:8))     disp('All the bits have value 1.')     end    All the bits have value 1.

Example 3 — Vector and Array Operations

Get the value of the second most significant bit of the number sequence 5 through 75, counting by tens:

bitget(5:10:75, [2 3 4 5 5 5 6 7])  ans =       0     1     1     0     0     1     0     1

Do the same, but using 2-by-4 matrices:

bitget([5 15 25 35; 45 55 65 75], ...         [2 3 4 5; 5 5 6 7])  ans =       0     1     1     0       0     1     0     1  
See Also

bitandbitcmpbitmaxbitorbitset,bitshift,bitxor

> c = bitget(2,5:-1:1)

c =

     0     0     0     1     0
 
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

dec2bin

Convert decimal to binary number in string

Syntax

str = dec2bin(d)
str = dec2bin(d,n)

Description

returns the

str = dec2bin(d) binary representation of d as a string. d must be a nonnegative integer smaller than 2^52.

str = dec2bin(d,n) produces a binary representation with at least n bits.

The output of dec2bin is independent of the endian settings of the computer you are using.

Examples

Decimal 23 converts to binary 010111:

dec2bin(23)  ans =      10111

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有