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

13377 questions
1000
votes
9 answers

What does the 'b' character do in front of a string literal?

Apparently, the following is the valid syntax: my_string = b'The string' I would like to know: What does this b character in front of the string mean? What are the effects of using it? What are appropriate situations to use it? I found a related…
Jesse Webb
  • 36,395
  • 25
  • 99
  • 138
920
votes
61 answers

How to count the number of set bits in a 32-bit integer?

8 bits representing the number 7 look like this: 00000111 Three bits are set. What are algorithms to determine the number of set bits in a 32-bit integer?
Matt Howells
  • 37,626
  • 19
  • 78
  • 101
627
votes
36 answers

Python int to binary string?

Are there any canned Python methods to convert an Integer (or Long) into a binary string in Python? There are a myriad of dec2bin() functions out on Google... But I was hoping I could use a built-in function / library.
Nate
  • 16,729
  • 25
  • 68
  • 93
466
votes
24 answers

What is “2's Complement”?

I'm in a computer systems course and have been struggling, in part, with Two's Complement. I want to understand it but everything I've read hasn't brought the picture together for me. I've read the wikipedia article and various other articles,…
405
votes
12 answers

Reading binary file and looping over each byte

In Python, how do I read in a binary file and loop over each byte of that file?
Jesse Vogt
  • 14,831
  • 14
  • 55
  • 71
377
votes
8 answers

How do you express binary literals in Python?

How do you express an integer as a binary number with Python literals? I was easily able to find the answer for hex: >>> 0x12AF 4783 >>> 0x100 256 and octal: >>> 01267 695 >>> 0100 64 How do you use literals to express binary in Python? Summary…
Justin Standard
  • 20,961
  • 22
  • 74
  • 89
329
votes
13 answers

Why do we use Base64?

Wikipedia says Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without…
Lazer
  • 79,569
  • 109
  • 264
  • 349
328
votes
13 answers

How to view files in binary from bash?

I would like to view the contents of a file in the current directory, but in binary from the command line. How can I achieve this?
adam_0
  • 6,139
  • 6
  • 37
  • 52
322
votes
3 answers

How to convert 'binary string' to normal string in Python3?

For example, I have a string like this(return value of subprocess.check_output): >>> b'a string' b'a string' Whatever I did to it, it is always printed with the annoying b' before the string: >>> print(b'a string') b'a string' >>> print(str(b'a…
Hanfei Sun
  • 39,245
  • 33
  • 107
  • 208
249
votes
12 answers

How to print (using cout) a number in binary form?

I'm following a college course about operating systems and we're learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two's complement…
Jesse Emond
  • 6,360
  • 7
  • 30
  • 36
216
votes
15 answers

How to compare binary files to check if they are the same?

What is the easiest way (using a graphical tool or command line on Ubuntu Linux) to know if two binary files are the same or not (except for the time stamps)? I do not need to actually extract the difference. I just need to know whether they are the…
sawa
  • 156,411
  • 36
  • 254
  • 350
210
votes
18 answers

Why prefer two's complement over sign-and-magnitude for signed numbers?

I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1? -1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1…
Ray
  • 3,188
  • 8
  • 23
  • 27
208
votes
14 answers

Converting integer to binary in python

In order to convert an integer to a binary, I have used this code : >>> bin(6) '0b110' and when to erase the '0b', I use this : >>> bin(6)[2:] '110' What can I do if I want to show 6 as 00000110 instead of 110?
Smith
  • 2,263
  • 3
  • 14
  • 12
203
votes
19 answers

Can I use a binary literal in C or C++?

I need to work with a binary number. I tried writing: const x = 00010000; But it didn't work. I know that I can use an hexadecimal number that has the same value as 00010000, but I want to know if there is a type in C++ for binary numbers and if…
hamza
  • 2,326
  • 3
  • 17
  • 14
202
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,282
  • 3
  • 22
  • 29
1
2 3
99 100