binary

Pythonic way to iterate over bits of integer

十年热恋 提交于 2021-02-05 20:21:30
问题 Let's a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1] 回答1: There's a trick for just getting the 1's out of the binary representation without having to iterate over all the intervening 0's: def bits(n): while n: b = n & (~n+1) yield b n ^= b >>> for b in bits(109): print(b) 1 4 8 32 64 回答2: My approach: def bits(number): bit = 1 while number >= bit: if number & bit: yield bit bit <<= 1 I don't think there is a builtin function for it. I also wonder

Pythonic way to iterate over bits of integer

旧城冷巷雨未停 提交于 2021-02-05 20:19:39
问题 Let's a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1] 回答1: There's a trick for just getting the 1's out of the binary representation without having to iterate over all the intervening 0's: def bits(n): while n: b = n & (~n+1) yield b n ^= b >>> for b in bits(109): print(b) 1 4 8 32 64 回答2: My approach: def bits(number): bit = 1 while number >= bit: if number & bit: yield bit bit <<= 1 I don't think there is a builtin function for it. I also wonder

Pythonic way to iterate over bits of integer

大城市里の小女人 提交于 2021-02-05 20:19:06
问题 Let's a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1] 回答1: There's a trick for just getting the 1's out of the binary representation without having to iterate over all the intervening 0's: def bits(n): while n: b = n & (~n+1) yield b n ^= b >>> for b in bits(109): print(b) 1 4 8 32 64 回答2: My approach: def bits(number): bit = 1 while number >= bit: if number & bit: yield bit bit <<= 1 I don't think there is a builtin function for it. I also wonder

How to write/read classes within classes to binary file C++

你说的曾经没有我的故事 提交于 2021-02-05 12:23:57
问题 I'm working on a project where I have to write a class that contains three other classes as private member variables to a binary file, where it can then be read back into variables to be used in the code. The code writes to the file, but I don't know if it is writing the correct info as when I try to read the file it reads in junk. I have included my current setup, does this look correct? If so, what could be going wrong, and if not, how can I fix this? If you need me to add any extra code,

Converting decimal to binary in assembler

好久不见. 提交于 2021-02-05 12:17:25
问题 I need help with my first program in assembler. I have to convert values entered by user from decimal to binary. I have no idea how can I show values as a decimal, and what should I do next. could anyone instruct me step by step what do next. .model small .stack 100h` .data txt1 db "Enter binary value:" ,10,13, "$" txt2 db "BIN: " ,10,13, "$" .code main proc mov ax, @data mov ds, ax ;clear screen mov ah,0fh int 10h mov ah,0 int 10h ;show first text mov ah, 9 mov dx, offset txt1 int 21h call

Converting decimal to binary in assembler

不打扰是莪最后的温柔 提交于 2021-02-05 12:16:31
问题 I need help with my first program in assembler. I have to convert values entered by user from decimal to binary. I have no idea how can I show values as a decimal, and what should I do next. could anyone instruct me step by step what do next. .model small .stack 100h` .data txt1 db "Enter binary value:" ,10,13, "$" txt2 db "BIN: " ,10,13, "$" .code main proc mov ax, @data mov ds, ax ;clear screen mov ah,0fh int 10h mov ah,0 int 10h ;show first text mov ah, 9 mov dx, offset txt1 int 21h call

Why do the bytes of a PNG image downloaded with reqwest differ from those downloaded with Python?

偶尔善良 提交于 2021-02-05 11:51:42
问题 I'm trying to use reqwest library to download a PNG file, but when I download it I see a strange behaviour respect other programming languages like: Python. For instance: let content = reqwest::get("https://www.google.es/images/searchbox/desktop_searchbox_sprites302_hr.png").await?; If I print the result as a bytes array ( println!("{:?}", content.text().await?.as_bytes() ); [ 191, 189, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 40, 0, 0, 0, 82, 8, 3, 0, 0, 0, 17, 191,

Given N bits, how many integers can be represented in binary?

。_饼干妹妹 提交于 2021-02-05 10:37:45
问题 Suppose you have 14 bits. How do you determine how many integers can be represented in binary from those 14 bits? Is it simply just 2^n? So 2^14 = 16384? Please note this part of the question: "how many INTEGERS can be represented in BINARY...". That's where my confusion lies in what otherwise seems like a fairly straightforward question. If the question was just asking how many different values or numbers can be represented from 14 bits, than yes, I'm certain it's just 2^n. 回答1: The answer

Octave Rounding and Evaluation Order

会有一股神秘感。 提交于 2021-02-05 08:57:05
问题 In Octave I obtain 1 - 0.05 -0.95 = 0 and 1 - 0.95 -0.05 = 4.1633e-17 I understand that it is caused by the order of evaluation combined with the approximate binary representation of 0.05 as 0.00(0011) and 0.95 as 0.11(1100) Could someone please give me the whole story or show me a link explaining it? ---EDIT: This question is not a duplicate of Why is 24.0000 not equal to 24.0000 in MATLAB?, which was identified by others as a possible duplicate. The latter deals with the rounded

Translation from binary into decimal numbers in C++

两盒软妹~` 提交于 2021-02-05 08:10:30
问题 I tried to build a function that calculates a binary number stored in a string into a decimal number stored in a long long . I'm thinking that my code should work but it doesn't. In this example for the binary number 101110111 the decimal number is 375 . But my output is completely confusing. Here is my code: #include <string> #include <stdio.h> #include <math.h> #include <iostream> #include <string.h> int main() { std::string stringNumber = "101110111"; const char *array = stringNumber.c_str