Jan 08 - Reverse Bits; Binary; Integer; Bits; 复习位运算和二进制表达

复习 二进制位操作(bit operation),32bit integer 取值范围:-2^31 -- 2^31-1

0 1 1 1 1 1 1 1 = 127
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 0 0 = 0
1 1 1 1 1 1 1 1 = −1
1 1 1 1 1 1 1 0 = −2
1 0 0 0 0 0 0 1 = −127
1 0 0 0 0 0 0 0 = −128

(2‘ compliment)二补数系统的最大优点是可以在加法减法处理中,不需因为数字的正负而使用不同的计算方式。只要一种加法电路就可以处理各种有号数加法,而且减法可以用一个数加上另一个数的二补数来表示,因此只要有加法电路及二补数电路即可完成各种有号数加法及减法,在电路设计上相当方便。

Bitwise and Bit Shift Operators

The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist.

The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a bytecontains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

The bitwise & operator performs a bitwise AND operation.

The bitwise ^ operator performs a bitwise exclusive OR operation.

The bitwise | operator performs a bitwise inclusive OR operatio

代码:

public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int result = 0;
for(int i = 32; i > 0; i--){
result = result << 1;
result = result + (n & 1);
n = n >>> 1;
}
return result;
}
}

时间: 2024-10-10 12:44:27

Jan 08 - Reverse Bits; Binary; Integer; Bits; 复习位运算和二进制表达的相关文章

【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should r

Multi-processor having shared memory, private cache memories, and invalidate queues having valid bits and flush bits for serializing transactions

Multi-processor systems are often implemented using a common system bus as the communication mechanism between CPU, memory, and I/O adapters. It is also common to include features on each CPU module, such as cache memory, that enhance the performance

java中Integer包装类的详细讲解(java二进制操作,所有进制转换)

程序员都很懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往需要操作Integer,或者各种进制的转换等等.我今天就为大家详细讲解一下Integer的使用吧.看代码: package com.herman.test; public class IntegerTest { public static void main(String[] args) { System.out.println("Integer中的常量***************************

java中Integer包装类的具体解说(java二进制操作,全部进制转换)

程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: package com.herman.test; public class IntegerTest { public static void main(String[] args) { System.out.println("Integer中的常量**************************

UVA11127- Triple-Free Binary Strings(DFS+位运算)

题目链接 题意:给出长度为n的字符串,字符串由'1','0','*'组成,其中'*'可以任意替换为'1','0',求不存在连续3个相同子串的字符串的最多个数. 思路:我们可以利用二进制的形式来表示字符串,进行DFS.利用位运算的左移来表示在'*'位置上放置'1',注意在递归的过程中注意判断之否存在3个连续相同的子串. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <alg

Leetcode 190 Reverse Bits 位运算

反转二进制 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 uint32_t ans = 0; 5 for (int i = 0; i<32; ++i,n >>=1){ 6 ans = (ans<< 1)|(n & 1); 7 } 8 return ans; 9 } 10 };

leetcode_171题——Number of 1 Bits(位运算)

Number of 1 Bits Total Accepted: 38248 Total Submissions: 101730My Submissions Question Solution Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit int

Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向低位枚举,如果这一位a[i]是0,我们就在a[i]的右边找两个位置让它们按位与起来这位是1.那么,我们贪心的保留可以通过按位与凑出某个二进制数的最靠右的两个位置.这个可以通过dp的方式预处理出来.之后,我们枚举每一个数a[i],先找出它的哪些位是0,之后从高位到低位枚举,判断这一位是否可以变成1.如果之前已经

Jan 12 - Power of Two; Integer; Bit Manipulation;

Two's complement of integer: https://zh.wikipedia.org/wiki/%E4%BA%8C%E8%A3%9C%E6%95%B8 Bit Manipulation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html 代码: public class Solution { public boolean isPowerOfTwo(int n) { int count = 0