Js implementation of special locating anti-Principles and Examples _ javascript skills

Source: Internet
Author: User
Two hex and n (0 & lt; n & lt; 31) are input cyclically. hex is a hexadecimal number, what we need to do is to reverse the n-digit hex and output the corresponding results in hexadecimal form to Huawei for an interview; the interview process was neither clear nor checked. As soon as the results passed, the computer was asked to answer questions, which was a little unprepared. I am a front-end Java Web engineer. The basic underlying programming knowledge has long been unfamiliar. The question of this bitwise operation is very simple, and the principle is very clear to the author. However, it has not been used for bitwise operations for many years, java bit operations have never been performed, so the results are really not decent ......

The trial time is set to one hour. The language can be C or Java. It took me nearly three hours to use Java to barely answer the question ...... When I came back, I used JS to implement a simple version. Today, I sorted it out and pasted it.

The question is: cyclically input two numbers in each group hex and n (0 <= n <31). hex is a hexadecimal number, what we need to do is to reverse the nth bit of hex and output the corresponding result in hexadecimal format.

I will not go into details about the process of tossing for more than two hours. The implementation of js is provided here, which is a simple basic bit operation knowledge. The principle is to shift 1 to n places left by bit, and then it is just like the original number or:

Function bitwise (hex, n) {var num = parseInt (hex); num ^ = (1 <



Because the js Integer type is limited to 32 bits, the preceding sample code only supports n <31 (31st bits are signed bits ).

Related Article

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.