473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Binary

Is there any way to declare a variable as a binary so that if the
variable "var" holds the value of 1001, then ++var = 1010?

Sep 28 '05 #1
9 1917
* Gaijinco:

Is there any way to declare a variable as a binary so that if the
variable "var" holds the value of 1001, then ++var = 1010?


Binary, decimal, hexadecimal, roman numerals and so on are presentation
formats: how values are _presented_ by the system, or input to the system.

In C++ all built-in integer types are binary in the sense that the language
provides some operations that operate directly on the binary digits.

However, that does not mean they are necessarily binary in the sense of the
binary number system, although (with a slight modification for signed types)
that is a very reasonable assumption, and one extremely unlikely to be wrong.

Consider the following program:

#include <iostream> // std::cout
#include <ostream> // <<, std::endl
#include <bitset> // std::bitset

int main()
{
typedef std::bitset<4> FourBits;
int x;

x = 9; // Binary 1001.
std::cout << FourBits( x ) << std::endl;
++x;
std::cout << FourBits( x ) << std::endl;
}

The output here is 1001 and 1010, so '++x' does exactly what you want.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 28 '05 #2
"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Is there any way to declare a variable as a binary so that if the
variable "var" holds the value of 1001, then ++var = 1010?


Binary 1001 is not a value, but a representation. This does exactly what you
need:

int var = 9;
++var; // now the value is binary 1010

But no, you cannot use binary representation in the source code; only
decimal, hexadecimal, or octal are allowed. One way of getting the binary
representation using the standard library is through bitset:

#include <bitset>
#include <limits>
#include <iostream>

using namespace std;

int main()
{
int var = 9;
++var;

size_t const int_bits = numeric_limits<int>::digits;

cout << bitset<int_bits>(var) << '\n';
}

Ali

Sep 28 '05 #3
Gaijinco wrote:
Is there any way to declare a variable as a binary so that if the
variable "var" holds the value of 1001, then ++var = 1010?


unsigned long var = std::bitset("1001").to_ulong();
++var;
std::cout << std::bitset(var).to_string();

Of course you can just do

int var = 9;
++var; // now 'var' is 10, which is binary '1010'

V
Sep 28 '05 #4
> Is there any way to declare a variable as a binary so that if the
variable "var" holds the value of 1001, then ++var = 1010?


This reminds of a question I wanted to ask to this gruoup...

If one made a union like this (assuming that int is 32 bits long on this
particular platform):

union Integer32Union {
int value;
struct {
int bit0 : 1;
int bit1 : 1;
// (...)
int bit31 : 1;
} bits;
};

-and you made the allocation of instances of this union yourself (i.e. you
do not reinterpret-cast some int given to you), wouldn't the compiler
guarantee that the bits int the bit field were stored in exactly this order,
corresponding to the order of the bits in the int?

Or is this a big no-no?

Obviously issues like endian-ness and alignment would be subject for
discussion.

I'm just thinking that most compilers would be inclined to align an int(1),
and if you took into consideration the endian-ness of your particular
platform when ordering the bits in the bit field, you should be able to
extract the bits separately.

Of course this a question of curiosity - I would definately go for some good
ol' bit shifting if I wanted to extract bits from an int.

-Mogens
(1) which, to my knowledge, is the purpose of an int-type whose length you
cannot be certain of: it is always aligned, and has the length of one
machine word
Sep 28 '05 #5
Mogens Heller Jensen wrote:
[..]
If one made a union like this (assuming that int is 32 bits long on this
particular platform):

union Integer32Union {
int value;
struct {
int bit0 : 1;
int bit1 : 1;
// (...)
int bit31 : 1;
} bits;
};

-and you made the allocation of instances of this union yourself (i.e. you
do not reinterpret-cast some int given to you), wouldn't the compiler
guarantee that the bits int the bit field were stored in exactly this order,
corresponding to the order of the bits in the int?
No.
Or is this a big no-no?
The Standard defines only the use of one of the fields in a union at
a time. If you store 'value', you're only allowed to retrieve 'value'.
Retrieving 'bit0' or anything else from the union after storing 'value'
has undefined behaviour.
[...]


V
Sep 28 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:TA*****************@newsread1.mlpsca01.us.to. verio.net...
Mogens Heller Jensen wrote:
[..]
If one made a union like this (assuming that int is 32 bits long on this
particular platform):

union Integer32Union {
int value;
struct {
int bit0 : 1;
int bit1 : 1;
// (...)
int bit31 : 1;
} bits;
};

-and you made the allocation of instances of this union yourself (i.e.
you do not reinterpret-cast some int given to you), wouldn't the compiler
guarantee that the bits int the bit field were stored in exactly this
order, corresponding to the order of the bits in the int?


No.
Or is this a big no-no?


The Standard defines only the use of one of the fields in a union at
a time. If you store 'value', you're only allowed to retrieve 'value'.
Retrieving 'bit0' or anything else from the union after storing 'value'
has undefined behaviour.
[...]


V


That would be a "no-no"... :o)

Thanks.

By the way, I love this group!

-Mogens
Sep 29 '05 #7
Victor Bazarov wrote:
Mogens Heller Jensen wrote:

[snip]
wouldn't the compiler
guarantee that the bits int the bit field were stored in exactly this order,
corresponding to the order of the bits in the int?


No.


For more on the non-portable nature of bit-fields, see this post:

http://groups.google.com/group/comp....42e0895515b193

Cheers! --M

Sep 29 '05 #8
Mogens Heller Jensen wrote:
[...]
By the way, I love this group!


It's one of the best I've encountered so far as well.
Sep 29 '05 #9

"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Victor Bazarov wrote:
Mogens Heller Jensen wrote:

[snip]
> wouldn't the compiler
> guarantee that the bits int the bit field were stored in exactly this
> order,
> corresponding to the order of the bits in the int?


No.


For more on the non-portable nature of bit-fields, see this post:

http://groups.google.com/group/comp....42e0895515b193

Cheers! --M


Thanks for the link.

I guess I fall into one of those categories then, since I have used some
nasty bit field union things to do re-interpret casts between various fixed
point fractional types and bit fields to be able to set/get the bits
individually.

At the time it seemed so nice (and it worked for the compiler/platform I was
working on: Tasking/Motorola 563xx DSPs), but as it turns out it's a novice
optimization. And one does not want to get into the habit of doing
non-portable optimizations. It would probably be hard to track down those
bugs some day!

-Mogens
Sep 29 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
5
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
16
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.