473,406 Members | 2,345 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,406 software developers and data experts.

Macro hints

Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.

#define READ16(b) ((*(b)<<8) | *((b)+1))

I can't quite figure out how it works. Any hints would be appreciated.
Thanks!

Aug 1 '07 #1
6 1415
On Wed, 01 Aug 2007 05:13:41 -0700, inftoconc wrote:
Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.

#define READ16(b) ((*(b)<<8) | *((b)+1))

I can't quite figure out how it works. Any hints would be appreciated.
Thanks!
*(b)<<8 is *(b) (i.e. b[0]) shifted by eight bits to the left,
i.e. b[0] * 256.
(b[0] << 8) | b[1] is the bitwise or of its operands, which in
this case is their sum. So that macro is essentially equivalent
(assuming CHAR_BIT is 8) to
#define READ16(b) (256 * (b)[0] + (b)[1])
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 1 '07 #2
in*******@gmail.com writes:
Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.

#define READ16(b) ((*(b)<<8) | *((b)+1))

I can't quite figure out how it works. Any hints would be appreciated.
Thanks!
Evaluate it left to right.

It takes the byte at address b (*b) and then shifts the result left 8
bits (<<8). It then bitwise ors (|) the byte stored at the address B
points to offset by one byte.

I'm not sure if I should have been saying char above instead of byte :-;

It's to guarantee a 16 bit value from the buffer into an integer or
bigger with no endian conversions.
Aug 1 '07 #3
On Wed, 01 Aug 2007 14:48:18 +0200, Richard wrote:
in*******@gmail.com writes:
>Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.

#define READ16(b) ((*(b)<<8) | *((b)+1))

I can't quite figure out how it works. Any hints would be appreciated.
Thanks!

Evaluate it left to right.

It takes the byte at address b (*b) and then shifts the result left 8
bits (<<8). It then bitwise ors (|) the byte stored at the address B
points to offset by one byte.

I'm not sure if I should have been saying char above instead of byte :-;
<snip>
You should say byte. The macro will only work if bytes are unsigned.
If chars are signed, and b is a char*, the macro goes horribly wrong when
b[1] is negative.

Aug 1 '07 #4
Duncan Muirhead <dm***@csl.co.ukwrites:
On Wed, 01 Aug 2007 14:48:18 +0200, Richard wrote:
>in*******@gmail.com writes:
>>Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.

#define READ16(b) ((*(b)<<8) | *((b)+1))

I can't quite figure out how it works. Any hints would be appreciated.
Thanks!

Evaluate it left to right.

It takes the byte at address b (*b) and then shifts the result left 8
bits (<<8). It then bitwise ors (|) the byte stored at the address B
points to offset by one byte.

I'm not sure if I should have been saying char above instead of byte :-;
<snip>
You should say byte. The macro will only work if bytes are unsigned.
If chars are signed, and b is a char*, the macro goes horribly wrong when
b[1] is negative.
Who has a link to a concise definition of C and sizes of bits, bytes,
chars, or "how to talk portable C" etc preferably not the standard which
I find almost unreadable in most cases.

Aug 1 '07 #5
Richard wrote:
Duncan Muirhead <dm***@csl.co.ukwrites:
>On Wed, 01 Aug 2007 14:48:18 +0200, Richard wrote:
>>in*******@gmail.com writes:

Hi, I came across the macro definition (below) to read 16 bits out of
a buffer into an integer. b's type is byte.

#define READ16(b) ((*(b)<<8) | *((b)+1))

I can't quite figure out how it works. Any hints would be appreciated.
Thanks!
Evaluate it left to right.

It takes the byte at address b (*b) and then shifts the result left 8
bits (<<8). It then bitwise ors (|) the byte stored at the address B
points to offset by one byte.

I'm not sure if I should have been saying char above instead of byte :-;
<snip>
You should say byte. The macro will only work if bytes are unsigned.
If chars are signed, and b is a char*, the macro goes horribly wrong when
b[1] is negative.

Who has a link to a concise definition of C and sizes of bits, bytes,
chars, or "how to talk portable C" etc preferably not the standard which
I find almost unreadable in most cases.
Annex J of the Standard.
http://home.att.net/~jackklein/c/inttypes.html
http://nob.cs.ucdavis.edu/bishop/pap...urv1n4/port.ps
http://www.cpax.org.uk/prg/portable/c/index.php

Aug 1 '07 #6
santosh <sa*********@gmail.comwrites:
Richard wrote:
>>
Who has a link to a concise definition of C and sizes of bits, bytes,
chars, or "how to talk portable C" etc preferably not the standard which
I find almost unreadable in most cases.

Annex J of the Standard.
http://home.att.net/~jackklein/c/inttypes.html
http://nob.cs.ucdavis.edu/bishop/pap...urv1n4/port.ps
http://www.cpax.org.uk/prg/portable/c/index.php
Thanks Santosh.
Aug 2 '07 #7

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
5
by: newtophp2000 | last post by:
I am kind of confused about the way SQL Server 2000 handles the hints that users supply with their SQL statements. >From BOL, it seems that one can specify them with "WITH (...)" clauses in SQL...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
20
by: Jim Ford | last post by:
I understand that some compilers define a symbol that can be used anywhere in the code in order to find out, at run time, the name of the file where the code corresponding to the current execution...
4
by: Garry Freemyer | last post by:
I'm trying to convert this macro to a c# function but I have a big problem. It's on the LEFT side of an assignment statement and I am extremely flustered over this one because I'm a little rusty...
28
by: darren via AccessMonster.com | last post by:
Hi I've seen details on bypassing macro security with a bat file but has any one incorporated this with Tony Toews FE Updater? If so, I'd appreciate some guidance. As both open the database...
9
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
I am replacing a number of functions with macros. Some of these functions "do something" and then return a variable. This behaviour I have found that I can emulate using the comma operator: ...
7
by: Tehn Yit Chin | last post by:
Does anyone know of an equivalent macro to offset() that would work for bit fields? eg, struct abc { enable: 1; disable: 1; speed: 6;
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.