473,394 Members | 1,765 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,394 software developers and data experts.

value of bit

Hi there

given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?

Thanks a lot

Lazyworm
Aug 11 '08 #1
11 2687
"Lazyworm" <la*********@gmail.comwrote in message
news:18**********************************@l64g2000 hse.googlegroups.com...
Hi there

given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?
20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:

#include <limits.h /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

(If you don't have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.

** Posted from http://www.teranews.com **
Aug 11 '08 #2
In article <18**********************************@l64g2000hse. googlegroups.com>,
Lazyworm <la*********@gmail.comwrote:
>given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?
int somevariable = 123;
unsigned char varp = (unsigned char *)&somevariable;
Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
that can be used to access the individual bytes of the value.

Note the use of unsigned char, not of char. unsigned char is
guaranteed not to have any trap values or padding bits: provided
that the underlying data representation really is binary
(not, for example, trinary on some bizarre computer) then
unsigned char is the only type that you can use to be sure that you
can access the individual bits.

But be careful: varp[0] does not necessarily contain the
"most significant byte" of the value you wish to examine, and
it does not necessarily contain the "least significant byte" either:
varp[0] could end being something in the "middle" of how that
particular systems treats "int" values (seriously!). And there
can be padding bits in numeric values, and there can be padding
bits in structures that use bitfields, and there can be padding
bytes in structures -- so although you might be able to examine
the underlying bytes, it might not always be immediately clear what
those bytes *mean*.
--
"After all, what problems has intellectualism ever solved?"
-- Robert Gilman
Aug 11 '08 #3
Walter Roberson wrote:
) In article <18**********************************@l64g2000hse. googlegroups.com>,
) Lazyworm <la*********@gmail.comwrote:
)
)>given a primitive type variable, such as "char" or "int", how do i get
)>the value of each bit in the char? or, how do i get the binary form of
)>the variable?
)
) int somevariable = 123;
) unsigned char varp = (unsigned char *)&somevariable;
) Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
) that can be used to access the individual bytes of the value.

NB: The OP wanted bits, not bytes.

Furthermore, using shift and mask operations is much more portable, and
any compiler worth its salt will optimize it to similar performance anyway.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Aug 11 '08 #4
On Aug 11, 11:28 pm, "Dann Corbit" <dcor...@connx.comwrote:
"Lazyworm" <lazyworm...@gmail.comwrote in message
[...] how do i get the binary form of the variable?

20.8: How can I implement sets or arrays of bits?
<snip rest>

Not the same question.
Aug 11 '08 #5
Lazyworm wrote:
Hi there

given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?
/* BEGIN output from bitstr.c */

0.333333 = 00111110101010101010101010101011
0.666667 = 00111111001010101010101010101011
1.333333 = 00111111101010101010101010101011
2.666667 = 01000000001010101010101010101011
5.333333 = 01000000101010101010101010101011
10.666667 = 01000001001010101010101010101011
21.333334 = 01000001101010101010101010101011
42.666668 = 01000010001010101010101010101011

/* END output from bitstr.c */

/* BEGIN bitstr.c */

#include <limits.h>
#include <stdio.h>

#define STRING "%15f = %s\n"
#define E_TYPE float
#define INITIAL (1.0f / 3)
#define FINAL 50
#define INC(E) ((E) *= 2)

typedef E_TYPE e_type;

void bitstr(char *str, const void *obj, size_t n);

int main(void)
{
e_type e;
char ebits[CHAR_BIT * sizeof e + 1];

puts("\n/* BEGIN output from bitstr.c */\n");
for (e = INITIAL; FINAL >= e; INC(e)) {
bitstr(ebits, &e, sizeof e);
printf(STRING, e, ebits);
}
puts("\n/* END output from bitstr.c */");
return 0;
}

void bitstr(char *str, const void *obj, size_t n)
{
unsigned mask;
const unsigned char *const byte = obj;

while (n-- != 0) {
mask = ((unsigned char)-1 >1) + 1;
do {
*str++ = (char)(mask & byte[n] ? '1' : '0');
mask >>= 1;
} while (mask != 0);
}
*str = '\0';
}

/* END bitstr.c */
--
pete
Aug 11 '08 #6
On Aug 12, 12:48 am, pete <pfil...@mindspring.comwrote:
<snip>
#define STRING "%15f = %s\n"
#define E_TYPE float
#define INITIAL (1.0f / 3)
#define FINAL 50
#define INC(E) ((E) *= 2)

typedef E_TYPE e_type;
<snip>

Aren't identifiers starting with E reserved? Or that's only if you
include <errno.h>?
Aug 11 '08 #7
"Dann Corbit" <dc*****@connx.comwrites:
"Lazyworm" <la*********@gmail.comwrote in message
news:18**********************************@l64g2000 hse.googlegroups.com...
>Hi there

given a primitive type variable, such as "char" or "int", how do i get
the value of each bit in the char? or, how do i get the binary form of
the variable?

20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:

#include <limits.h /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

(If you don't have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.
Note that the "If you don't have <limits.h>" clause is very unlikely
to apply these days.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 11 '08 #8
<vi******@gmail.comwrote in message
news:fa**********************************@8g2000hs e.googlegroups.com...
On Aug 11, 11:28 pm, "Dann Corbit" <dcor...@connx.comwrote:
>"Lazyworm" <lazyworm...@gmail.comwrote in message
[...] how do i get the binary form of the variable?

20.8: How can I implement sets or arrays of bits?
<snip rest>

Not the same question.
It answers the question exactly, if you bother to examine the source code.

** Posted from http://www.teranews.com **
Aug 11 '08 #9
vi******@gmail.com writes:
On Aug 12, 12:48 am, pete <pfil...@mindspring.comwrote:
<snip>
>#define STRING "%15f = %s\n"
#define E_TYPE float
#define INITIAL (1.0f / 3)
#define FINAL 50
#define INC(E) ((E) *= 2)

typedef E_TYPE e_type;

<snip>

Aren't identifiers starting with E reserved? Or that's only if you
include <errno.h>?
Only identifiers starting with E followed by either a digit or an
uppercase letter. E and E_TYPE are ok.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 12 '08 #10
On Mon, 11 Aug 2008 21:37:07 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote in comp.lang.c:
In article <sl********************@snail.stack.nl>,
Willem <wi****@stack.nlwrote:
Walter Roberson wrote:
) In article <18**********************************@l64g2000hse. googlegroups.com>,
) Lazyworm <la*********@gmail.comwrote:
)>given a primitive type variable, such as "char" or "int", how do i get
)>the value of each bit in the char? or, how do i get the binary form of
)>the variable?
) int somevariable = 123;
) unsigned char varp = (unsigned char *)&somevariable;
) Then varp[0] to varp[sizeof(somevariable)-1] are unsigned char
) that can be used to access the individual bytes of the value.
NB: The OP wanted bits, not bytes.

The original pointer wanted "the binary form of the variable".
But if the OP already has a char or an int, as he says he has, he
already has "the binary form of the variable".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 12 '08 #11
On Aug 12, 4:35 am, Keith Thompson <ks...@mib.orgwrote:
vipps...@gmail.com writes:
On Aug 12, 12:48 am, pete <pfil...@mindspring.comwrote:
<snip>
#define STRING "%15f = %s\n"
#define E_TYPE float
#define INITIAL (1.0f / 3)
#define FINAL 50
#define INC(E) ((E) *= 2)
typedef E_TYPE e_type;
<snip>
Aren't identifiers starting with E reserved? Or that's only if you
include <errno.h>?

Only identifiers starting with E followed by either a digit or an
uppercase letter. E and E_TYPE are ok.
Ah, thanks. :)
Aug 12 '08 #12

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

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
3
by: otto | last post by:
i need to read a variable in a javascript and translate it to a form in html the javascript variable is: <SCRIPT LANGUAGE='JavaScript'>RF2N('Total');</script> and i need to put that...
3
by: Eric Chang | last post by:
I was working on this simple form with radio boxes. And when I click on one of the radio box, it tell me the value is "undefined" Why is that ? I did defined the value of each radio box: ...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
4
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email...
7
by: matthew_carver | last post by:
Hello, I have an ASP page that loops through a SQL Server 2000 table, then downloads an Excel sheet the users can save, etc. Works fine, except, I see that in one particular "comments" field the...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
0
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL,...
1
by: cbellew | last post by:
Hi guys, I have a problem with an option group and a two corresponding text boxes. When the user chooses an option value i want the text boxes to populate with text dependent on the choice made....
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.