bit

【比赛题解】CSP2019 简要题解

廉价感情. 提交于 2019-12-05 04:58:15
D1T1 code 签到题,大家都会。 可以从高位往低位确定,如果遇到 \(1\) ,则将排名取反一下。 注意要开 unsigned long long 。 #include <bits/stdc++.h> typedef unsigned long long u64; const int MaxN = 100; u64 n, K; bool ans[MaxN]; inline void solve(u64 dep, u64 k) { if (dep == 0) return; u64 lsze = 1ull << (dep - 1); if (k < lsze) { ans[dep] = false; solve(dep - 1, k); } else { ans[dep] = true; solve(dep - 1, lsze - (k - lsze) - 1); } } int main() { freopen("code.in", "r", stdin); freopen("code.out", "w", stdout); std::cin >> n >> K; solve(n, K); for (int i = n; i >= 1; --i) putchar(ans[i] ? '1' : '0'); return 0; } D1T2 brackets 简单题,大家都会。

Bit parity code for odd number of bits

落爺英雄遲暮 提交于 2019-12-05 04:31:15
I am trying to find the parity of a bitstring so that it returns 1 if x has an odd # of 0's. I can only use basic bitwise operations and what I have so far passes most of the tests, but I'm wondering 2 things: Why does x ^ (x + ~1) work? I stumbled upon this, but it seems to give you 1 if there are an odd number of bits and something else if even. Like 7^6 = 1 because 7 = 0b0111 Is this the right direction of problem solving for this? I'm assuming my problem is stemming from the first operation, specifically (x + ~1) because it would overflow certain 2's complement numbers. Thanks Code: int

how is data stored at bit level according to “Endianness”?

余生颓废 提交于 2019-12-05 03:38:59
问题 I read about Endianness and understood squat... so I wrote this main() { int k = 0xA5B9BF9F; BYTE *b = (BYTE*)&k; //value at *b is 9f b++; //value at *b is BF b++; //value at *b is B9 b++; //value at *b is A5 } k was equal to A5 B9 BF 9F and (byte)pointer " walk " o/p was 9F BF b9 A5 so I get it bytes are stored backwards...ok. ~ so now I thought how is it stored at BIT level... I means is "9f"(1001 1111) stored as "f9"(1111 1001)? so I wrote this int _tmain(int argc, _TCHAR* argv[]) { int k

spring boot1.1 idea + springboot + mybatis(mybatis-generator) +mysql +html实现简单的登录注册

微笑、不失礼 提交于 2019-12-05 02:20:56
前言 这两年springboot比较火,而我平时的工作中不怎么使用spring boot,所以工作之余就自己写写项目练练手,也跟大家一起学习。 打算从最开始的搭架子,登录注册,到后台管理的增删改查,业务逻辑,引用权限框架,如果能一直坚持下去就再增加缓存,中间件,消息队列,数据库方面分库分表,负载均衡这些比较主流的东西都玩一遍。 页面基本上都是在网上找的免费模板,也会借鉴一下其他的博客,如果引用较多会挂上原文链接。 spring boot 这里引用一篇阿里云的文章,有兴趣的朋友可以去看一看,是spring boot的介绍和使用。 https://yq.aliyun.com/articles/495578 正文 创建项目,用的JDK版本是1.8, 项目结构 js css这些资源要放在static包下,我放别的地方引用不进去,后文再说。 页面位置 pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache

Naudio - Convert 32 bit wav to 16 bit wav

半城伤御伤魂 提交于 2019-12-05 02:11:33
问题 I've been trying to convert a 32 bit stereo wav to 16 bit mono wav. I use naudio to capture the sound I and thought that using just the two of four more significant bytes will work. Here is the DataAvailable implementation: void _waveIn_DataAvailable(object sender, WaveInEventArgs e) { byte[] newArray = new byte[e.BytesRecorded / 2]; short two; for (int i = 0, j = 0; i < e.BytesRecorded; i = i + 4, j = j + 2) { two = (short)BitConverter.ToInt16(e.Buffer, i + 2); newArray[j] = (byte)(two &

How does Java calculate negative numbers?

↘锁芯ラ 提交于 2019-12-05 00:30:16
I use the ~ operation for bit manipulation, and I'm just wondering how Java calculates the negative number? I checked the Java documentation: "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 byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111"." So if int a = 60 (0011 1100) , then int c = ~a (1100 0011) . The question is, how Java calculate negative numbers so that 1100 0011 = -61 ? The only way

Hamming distance between two binary strings not working

强颜欢笑 提交于 2019-12-04 23:18:47
问题 I found an interesting algorithm to calculate hamming distance on this site: def hamming2(x,y): """Calculate the Hamming distance between two bit strings""" assert len(x) == len(y) count,z = 0,x^y while z: count += 1 z &= z-1 # magic! return count The point is that this algorithm only works on bit strings and I'm trying to compare two strings that are binary but they are in string format, like '100010' '101000' How can I make them work with this algorithm? 回答1: Implement it: def hamming2(s1,

数据结构与算法(位运算)

有些话、适合烂在心里 提交于 2019-12-04 20:43:18
位运算 内存中的数据,最终的存储方式都是二进制,位运算就是对整数在内存的二进制位进行操作。 按位与 & 两个整数进行按位与运算,相同二进制位的数字如果都是是,则结果为1,有一个为0,则结果为0 下面是 3 & 7 的计算过程 二进制 整数 0 1 1 3 1 1 1 7 0 1 1 3(结果) 3 & 7 = 3 按位或 | 两个整数进行按位或运算,相同二进制位的数字如果有一个为1,则结果为1,都为0,则结果为0 下面是 5 | 8 的计算过程 二进制 整数 0 1 0 1 5 1 0 0 0 8 1 1 0 1 13(结果) 5 | 8 = 13 左移 << 二进制向左移动n位,在后面添加n个0 下面的 3 << 1 的计算过程 二进制 整数 1 1 3 1 1 0 6 3<<1 = 6 练习:一组数,内容为 3,9,19,20 ,请用一个整数来表示这四个数 var value = 0 value = value | 1<<3 value = value | 1<<9 value = value | 1<<19 value = value | 1<<20 console.log(value) 程序输出结果为:1573384 bitmap 新的实现方式 经过前面一系列的分析和位运算学习,现在我们要重新设计一个类,实现 addMember 和 isExist 方法,用更快的速度

Apple SIP简介及在Clover中如何控制

牧云@^-^@ 提交于 2019-12-04 16:40:17
Apple SIP简介及在Clover中如何控制 来源 http://www.yekki.me/apple-sip-overview-and-how-to-disable-it-in-clover/ 什么是Apple SIP Apple SIP(System Integrity Protection)机制是OSX 10.11开始启用的一套关键的安全保护技术体系。 SIP技术的整个体系主要分为: 文件系统保护(Filesystem protection) 对于系统文件通过沙盒限制root权限,比如:就算你有root根限,也无法往/usr/bin目录写入。 运行时保护(Runtime protection) 受保护的关键系统进程在运行状态下无法被代码注入,挂调试器调试,以及限制内核调试等 内核扩展签名(Kext signing) 10.10中强制要求签名,要想绕过这个限制,就必需加入启动参数“kext-dev-mode=1”(10.11 DB5开始,”rootless=0”的启动参数也被废除了),这个启动参数在10.11中被废除。另外,10.11官方要求第三方kext必须被安装至/Library/Extensions。 Apple官方如何对SIP保护技术进行配置 进入10.11的安装程序或Recovery HD 使用其中所带的终端进行相关操作。在此环境下,由于特殊启动标志位的存在

Python读字节某一位的值,设置某一位的值,二进制位操作

落爺英雄遲暮 提交于 2019-12-04 16:28:17
Python读字节某一位的值,设置某一位的值,二进制位操作   在物联网实际应用项目开发中,为了提升性能,与设备端配合,往往最终使用的是二进制字节串方式进行的通信协议封装,更会把0和1、True和False、Yes和No这样的布尔值每8个只占用一个字节,用字节中的位来表示。减少传输量,减少对网络稳定性的要求。这就带来了要怎么读某个字节中每一位的值和怎么设置每一位的值的问题。   这几天再写培训演示代码,顺便就写了两个函数,解决字节位值读写问题,现在分享给大家。   下面直接上代码,在Python3上测试通过: #!/usr/bin/env python # -*- coding: utf-8 -*- def get_bit_val(byte, index): """ 得到某个字节中某一位(Bit)的值 :param byte: 待取值的字节值 :param index: 待读取位的序号,从右向左0开始,0-7为一个完整字节的8个位 :returns: 返回读取该位的值,0或1 """ if byte & (1 << index): return 1 else: return 0 def set_bit_val(byte, index, val): """ 更改某个字节中某一位(Bit)的值 :param byte: 准备更改的字节原值 :param index: 待更改位的序号