Questions tagged [bit]

A bit is a single binary digit.

A bit refers to a single binary digit, the smallest possible amount of information. In print is usually represented as either 0 or 1, with many different representations in technology, like the presence or absence of an electric current or magnetic field.

Eight bits form a single , although historically the term has been used for other sizes as well.

2508 questions
312
votes
11 answers

max value of integer

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767. In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647. I do not understand how the range is different in Java, even…
stackuser
  • 3,841
  • 4
  • 17
  • 25
160
votes
8 answers

Imply bit with constant 1 or 0 in SQL Server

Is it possible to express 1 or 0 as a bit when used as a field value in a select statement? e.g. In this case statement (which is part of a select statement) ICourseBased is of type int. case when FC.CourseId is not null then 1 else 0 end as…
Damien McGivern
  • 3,664
  • 3
  • 25
  • 21
112
votes
6 answers

What is the difference between BIT and TINYINT in MySQL?

In which cases would you use which? Is there much of a difference? Which I typically used by persistence engines to store booleans?
carrier
  • 29,579
  • 22
  • 74
  • 99
89
votes
7 answers

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { …
dewald
  • 4,633
  • 7
  • 33
  • 40
67
votes
11 answers

What USEFUL bitwise operator code tricks should a developer know about?

I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more…
non sequitor
  • 16,626
  • 9
  • 42
  • 59
65
votes
3 answers

What does (number & -number) mean in bit programming?

For example: int get(int i) { int res = 0; while (i) { res = (res + tree[i]) % MOD; i -= ( (i) & (-i) ); } return res; } A tree update function: void update(int i, int val) { while (i <= m) { tree[i] =…
SwadhIn
  • 697
  • 9
  • 19
61
votes
11 answers

Implement division with bit-wise operator

How can I implement division using bit-wise operators (not just division by powers of 2)? Describe it in detail.
TimeToCodeTheRoad
  • 6,146
  • 13
  • 50
  • 69
55
votes
4 answers

Really 1 KB (KiloByte) equals 1024 bytes?

Until now I believed that 1024 bytes equals 1 KB (kilobyte) but I was reading on the internet about decimal and binary system. So, actually 1024 bytes = 1 KB would be the correct way to define or simply there is a general confusion?
SamYan
  • 1,449
  • 1
  • 17
  • 36
54
votes
18 answers

Why is number of bits always(?) a power of two?

We have 8-bit, 16-bit, 32-bit and 64-bit hardware architectures and operating systems. But not, say, 42-bit or 69-bit ones. Why? Is it something fundamental that makes 2^n bits a better choice, or is just about compatibility with existing systems?…
Joonas Pulakka
  • 34,943
  • 25
  • 103
  • 165
53
votes
7 answers

Can't see MySQL BIT field value when using SELECT

my_table contains the enabled field which is defined as: enabled BIT NOT NULL DEFAULT 0. This table has multiple rows with enabled = b'0', and multiple rows with enabled = b'1'. However, both this: SELECT * from my_table WHERE enabled = b'0'; and…
Misha Moroshko
  • 148,413
  • 200
  • 467
  • 700
52
votes
9 answers

Switch on Enum (with Flags attribute) without declaring every possible combination?

how do i switch on an enum which have the flags attribute set (or more precisely is used for bit operations) ? I want to be able to hit all cases in a switch that matches the values declared. The problem is that if i have the following…
MartinF
  • 5,721
  • 5
  • 38
  • 28
51
votes
16 answers

Bitwise operator for simply flipping all bits in an integer?

I have to flip all bits in a binary representation of an integer. Given: 10101 The output should be 01010 What is the bitwise operator to accomplish this when used with an integer? For example, if I were writing a method like int flipBits(int…
Naftuli Kay
  • 75,812
  • 80
  • 244
  • 374
51
votes
8 answers

Divide by 10 using bit shifts?

Is it possible to divide an unsigned integer by 10 by using pure bit shifts, addition, subtraction and maybe multiply? Using a processor with very limited resources and slow divide.
Thomas O
  • 5,186
  • 11
  • 39
  • 58
50
votes
4 answers

How to get the value of a bit at a certain position from a byte?

If I have a byte, how would the method look to retrieve a bit at a certain position? Here is what I have know, and I don't think it works. public byte getBit(int position) { return (byte) (ID >> (position - 1)); } where ID is the name of the…
Glen654
  • 1,012
  • 3
  • 13
  • 18
46
votes
8 answers

How to read/write arbitrary bits in C/C++

Assuming I have a byte b with the binary value of 11111111 How do I for example read a 3 bit integer value starting at the second bit or write a four bit integer value starting at the fifth bit?
dtech
  • 44,350
  • 16
  • 93
  • 165
1
2 3
99 100