Some tricks in binary processing

Source: Internet
Author: User

In binary and decimal processing, sometimes small tricks are useful.

1. Convert decimal number to binary number

(1) in MATLAB, there is a function dec2bin, you can convert a positive integer to 2 binary

(2) For negative numbers there is a conclusion: n-bit binary negative X's complement corresponding to the unsigned number is 2N +x

Example: Signed original code: 10,010 binary:-1

Anti-code: 1110

Complement: 1111 unsigned number: 15

15 = 24 + (-1);

This allows for quick knowledge of the 4-bit binary representation of-5 's complement of unsigned numbers of 16-5=11 that is 1011

2. Convert negative binary complement to decimal

(1) The sign bit is removed, and the remaining bits are reversed plus one, the resulting number is the absolute value of the binary complement of negative numbers.

(2) directly to the two complement to take the opposite plus one;

(3) 1011 (-1) *23+0*22+1*21+1*20 = 5

Problem:

Some tricks in binary processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.