Questions tagged [binary]

Binary, the base-2 numeral system, represents numbers using two symbols: 0 and 1. For compiled computer programs, use the "executable" tag instead.

Binary is the base-2 positional numeral system. It represents numbers using two symbols: 0 and 1.

Computers use binary in digital circuitry using logic gates, where the symbols translate to states of off (0) and on (1).

Here are binary numbers corresponding to 0 to 10 in the decimal system:

Decimal   Binary
-------   ------
      0        0
      1        1
      2       10
      3       11
      4      100
      5      101
      6      110
      7      111
      8     1000
      9     1001
     10     1010

Other tags

14710 questions
230
votes
12 answers

C# binary literals

Is there a way to write binary literals in C#, like prefixing hexadecimal with 0x? 0b doesn't work. If not, what is an easy way to do it? Some kind of string conversion?
toxvaerd
  • 3,622
  • 3
  • 24
  • 29
215
votes
14 answers

Convert a binary NodeJS Buffer to JavaScript ArrayBuffer

How can I convert a NodeJS binary buffer into a JavaScript ArrayBuffer?
Drake Amara
  • 3,072
  • 3
  • 19
  • 18
205
votes
17 answers

Why does Git treat this text file as a binary file?

I wonder why git tells me this? $ git diff MyFile.txt diff --git a/MyFile.txt b/MyFile.txt index d41a4f3..15dcfa2 100644 Binary files a/MyFile.txt and b/MyFile.txt differ Aren't they text files? I have checked the .gitattributes and it is empty.…
nacho4d
  • 43,720
  • 45
  • 157
  • 240
205
votes
19 answers

Converting an int to a binary string representation in Java?

What would be the best way (ideally, simplest) to convert an int to a binary string representation in Java? For example, say the int is 156. The binary string representation of this would be "10011100".
Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
195
votes
6 answers

Tool for comparing 2 binary files in Windows

I need a tool to compare 2 binaries. The files are quite large. Some freeware or trial tools I found on the Internet are not convenient to use for large files. Can you recommend me some tools?
mustafa
  • 3,605
  • 7
  • 34
  • 56
194
votes
8 answers

How to convert a string or integer to binary in Ruby?

How do you create integers 0..9 and math operators + - * / in to binary strings. For example: 0 = 0000, 1 = 0001, ... 9 = 1001 Is there a way to do this with Ruby 1.8.6 without using a library?
mcmaloney
  • 2,374
  • 3
  • 19
  • 19
191
votes
8 answers

Convert decimal to binary in python

Is there any module or function in python I can use to convert a decimal number to its binary equivalent? I am able to convert binary to decimal using int('[binary_value]',2), so any way to do the reverse without writing the code to do it myself?
Paul
  • 1,959
  • 2
  • 12
  • 3
177
votes
11 answers

Are the shift operators (<<, >>) arithmetic or logical in C?

In C, are the shift operators (<<, >>) arithmetic or logical?
littlebyte
  • 1,789
  • 2
  • 11
  • 5
171
votes
13 answers

Fast way of counting non-zero bits in positive integer

I need a fast way to count the number of bits in an integer in python. My current solution is bin(n).count("1") but I am wondering if there is any faster way of doing this?
zidarsk8
  • 3,088
  • 4
  • 23
  • 30
167
votes
9 answers

How to convert string to binary?

I am in need of a way to get the binary representation of a string in python. e.g. st = "hello world" toBinary(st) Is there a module of some neat way of doing this?
user1090614
  • 2,575
  • 6
  • 22
  • 27
164
votes
8 answers

Reading a binary file with python

I find particularly difficult reading binary file with Python. Can you give me a hand? I need to read this file, which in Fortran 90 is easily read by int*4 n_particles, n_groups real*4 group_id(n_particles) read (*) n_particles, n_groups read (*)…
Brian
  • 13,996
  • 19
  • 70
  • 94
151
votes
8 answers

Reading and writing binary file

I'm trying to write code to read a binary file into a buffer, then write the buffer to another file. I have the following code, but the buffer only stores a couple of ASCII characters from the first line in the file and nothing else. int…
nf313743
  • 4,129
  • 8
  • 48
  • 63
151
votes
22 answers

Convert hex to binary

I have ABC123EFFF. I want to have 001010101111000001001000111110111111111111 (i.e. binary repr. with, say, 42 digits and leading zeroes). How?
aaaaaaaaaaaaaaaaaaa
147
votes
21 answers

Is it safe to use -1 to set all bits to true?

I've seen this pattern used a lot in C & C++. unsigned int flags = -1; // all bits are true Is this a good portable way to accomplish this? Or is using 0xffffffff or ~0 better?
hyperlogic
  • 7,525
  • 7
  • 39
  • 32
147
votes
17 answers

How to get 0-padded binary representation of an integer in java?

for example, for 1, 2, 128, 256 the output can be (16 digits): 0000000000000001 0000000000000010 0000000010000000 0000000100000000 I tried String.format("%16s", Integer.toBinaryString(1)); it puts spaces for left-padding: ` 1' How…
khachik
  • 28,112
  • 9
  • 59
  • 94